[NSWI004] [Assignment3] Need help with context switch

Tomáš Drozdík drozdik.tomas at gmail.com
Fri Dec 6 13:28:50 CET 2019


Hi,

I'm a bit behind in my assignments and I need a help.
In 3rd assignment when we need to schedule threads I'm not sure how to
deal with a thread which yields.

I assume that I need to memorize it's top of the stack so that
function thread_switch_to(thread_t*) can properly call
cpu_switch_context with saved top of the stack storing contents of all
registers onto it including current rip.
My problem with this is that at the moment a thread calls yield a new
stack frame is allocated for the thread_yield function.
However when the thread which yielded is rescheduled again it needs to
set its RIP to the instruction following the call to yield, but I'm
not sure how can I determine this instruction pointer.

FYI my thread_yield() implementation only calls
scheduler_schedule_next() which determines next thread to run and
calls thread_switch_to on this thread.
thread_t structure does

```
void thread_yield(void) {
    scheduler_schedule_next();
}

void thread_switch_to(thread_t* thread) {
    thread_t* current_thread = thread_get_current();
    current_thread->stack_top = (unative_t)debug_get_stack_pointer();
    void* stack_top_old = (void*)current_thread->stack_top;

    void* stack_top_new = (void*)thread->stack_top;

    cpu_switch_context(&stack_top_old, &stack_top_new, 1);
}
```

For an first switch to given thread a context_t is allocated at the
top of the stack and thread->stack_top is set to this context_t
structure so that thread_switch_to correctly calls entry function but
after a yield and reschedule thread->stack_top with dump of all
registers is at the top of the stack but RIP points to current
instruction in thread_switch_to function which I believe is incorrect.

Do I need to set RIP somehow manually i.e. add to it so that it skips
following call or something like that?

Sincerly
Tomáš Drozdík


More information about the NSWI004 mailing list