Skip to content

Commit 9b3889e

Browse files
committed
scheduler now crashes
1 parent 88379e3 commit 9b3889e

File tree

5 files changed

+40
-53
lines changed

5 files changed

+40
-53
lines changed

arch/wasm/include/asm/thread_info.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
struct thread_info {
1212
unsigned long flags;
1313
int preempt_count;
14-
// struct task_struct *from_sched;
14+
struct task_struct *from_sched;
1515
// unsigned int cpu;
1616
unsigned int instance_id;
1717
void* jmpbuf;

arch/wasm/kernel/process.c

Lines changed: 39 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -12,84 +12,76 @@ void longjmp(void *buf, int val) __attribute__((noreturn));
1212
static void *get_stack_pointer(void)
1313
{
1414
void *ptr;
15-
__asm("global.get __stack_pointer\n"
16-
"local.set %0"
17-
: "=r"(ptr));
15+
__asm__ volatile("global.get __stack_pointer\n"
16+
"local.set %0"
17+
: "=r"(ptr));
1818
return ptr;
1919
}
2020

2121
static void __always_inline set_stack_pointer(void *ptr)
2222
{
23-
__asm("local.get %0\n"
24-
"global.set __stack_pointer" ::"r"(ptr));
23+
__asm__ volatile("local.get %0\n"
24+
"global.set __stack_pointer" ::"r"(ptr));
2525
}
2626

27-
struct task_struct *__switch_to(struct task_struct *from,
28-
struct task_struct *to)
27+
static struct task_struct *prev = &init_task;
28+
29+
inline static struct task_struct *__switch_to_inner(struct task_struct *from,
30+
struct task_struct *to)
2931
{
3032
struct pt_regs *from_regs = task_pt_regs(from);
3133
struct pt_regs *to_regs = task_pt_regs(to);
3234
struct thread_info *from_info = task_thread_info(from);
3335
struct thread_info *to_info = task_thread_info(to);
3436

35-
// the following call crashes clang:
36-
// from_info->jmpbuf = kmalloc(40, 0);
37-
38-
// to_info->from_sched = from;
39-
current = to;
40-
from_regs->current_stack = get_stack_pointer();
41-
42-
4337
if (setjmp(from_info->jmpbuf) == 0) {
4438
set_stack_pointer(to_regs->current_stack);
45-
46-
schedule_tail(from);
47-
pr_info("switch %u -> %u\n", from->pid, to->pid);
48-
// if (to_info->from_sched)
49-
// schedule_tail(to_info->from_sched);
50-
// to_info->from_sched = NULL;
39+
40+
if (to_info->from_sched)
41+
schedule_tail(to_info->from_sched);
42+
to_info->from_sched = NULL;
5143

5244
if (to_regs->fn) {
5345
int (*fn)(void *) = to_regs->fn;
46+
int result;
47+
5448
to_regs->fn = NULL;
49+
pr_info("call %p(%p)\n", fn, to_regs->fn_arg);
5550

5651
// callback returns if the kernel thread execs a process?
57-
fn(to_regs->fn_arg);
52+
result = fn(to_regs->fn_arg);
53+
pr_info("call %p(%p) = %u\n", fn, to_regs->fn_arg,
54+
result);
5855
} else {
56+
pr_info("longjmp %p to %u\n", to_info->jmpbuf, to->pid);
5957
longjmp(to_info->jmpbuf, 1);
6058
}
59+
} else {
60+
pr_info("free %p\n", from_info->jmpbuf);
61+
kfree(from_info->jmpbuf);
6162
}
6263

63-
kfree(from_info->jmpbuf);
64-
65-
// pr_info("hi %u %u %u\n", from->pid, to->pid, current->pid);
66-
// return current_thread_info()->from_sched;
67-
return from;
68-
69-
// from_regs->current_stack = get_stack_pointer();
70-
71-
// if (setjmp(from_info->jmpbuf) == 0) {
72-
// pr_info("context switch %u %p\n"
73-
// " -> %u %p\n",
74-
// from->pid, from_regs->current_stack, to->pid,
75-
// to_regs->current_stack);
76-
77-
// set_stack_pointer(to_regs->current_stack);
78-
// pr_info("before jmp %u -> %u\n", current->pid, to->pid);
79-
// current = to;
64+
return prev;
65+
}
8066

81-
// schedule_tail(from);
67+
struct task_struct *__switch_to(struct task_struct *from,
68+
struct task_struct *to)
69+
{
70+
struct pt_regs *from_regs = task_pt_regs(from);
71+
struct pt_regs *to_regs = task_pt_regs(to);
72+
struct thread_info *from_info = task_thread_info(from);
73+
struct thread_info *to_info = task_thread_info(to);
8274

83-
// pr_info("regs: %p %p\n", to_regs, to_regs->fn);
75+
from_regs->current_stack = get_stack_pointer();
76+
from_info->jmpbuf = kmalloc(16, 0);
8477

85-
// longjmp(to_info->jmpbuf, 1);
86-
// }
78+
pr_info("alloc %p for %u\n", from_info->jmpbuf, from->pid);
8779

88-
// set_stack_pointer(from_regs->current_stack);
89-
// pr_info("resuming from jmp %u -> %u\n", current->pid, from->pid);
90-
// current = from;
80+
current = to;
81+
to_info->from_sched = prev;
82+
prev = from;
9183

92-
// return from;
84+
return __switch_to_inner(from, to);
9385
}
9486

9587
int copy_thread(struct task_struct *p, const struct kernel_clone_args *args)
@@ -103,8 +95,6 @@ int copy_thread(struct task_struct *p, const struct kernel_clone_args *args)
10395
if (!args->fn)
10496
panic("can't copy userspace thread"); // yet
10597

106-
// pr_info("copying thread %i %p\n", p->pid, childregs);
107-
10898
childregs->fn = args->fn;
10999
childregs->fn_arg = args->fn_arg;
110100

kernel/sched/core.c

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6511,7 +6511,6 @@ static void __sched notrace __schedule(unsigned int sched_mode)
65116511
*
65126512
* After this, schedule() must not care about p->state any more.
65136513
*/
6514-
pr_info("deactivating %s %lx %u %u\n", prev->comm, prev_state, prev->pid, prev->sched_class->rank);
65156514
deactivate_task(rq, prev, DEQUEUE_SLEEP | DEQUEUE_NOCLOCK);
65166515

65176516
if (prev->in_iowait) {

kernel/sched/fair.c

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6173,7 +6173,6 @@ static void dequeue_task_fair(struct rq *rq, struct task_struct *p, int flags)
61736173
bool was_sched_idle = sched_idle_rq(rq);
61746174

61756175
early_printk("dequeue %u %s\n", p->pid, p->comm);
6176-
wasm_breakpoint();
61776176

61786177
util_est_dequeue(&rq->cfs, p);
61796178

kernel/sched/swait.c

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,6 @@ void swake_up_locked(struct swait_queue_head *q)
2626
return;
2727

2828
curr = list_first_entry(&q->task_list, typeof(*curr), task_list);
29-
pr_info("curr: %p %p\n", curr, &curr->task);
3029
wake_up_process(curr->task);
3130
list_del_init(&curr->task_list);
3231
}

0 commit comments

Comments
 (0)