0
votes

I'm learning and writing operating system. And now I have learned about how to switch two processes to make my OS support multi process.

When I first program this feature, two processes running on my OS first running correctly but than they run strangely...

I know that this is because two processes' EIP or some other registers were saved incorrectly...

I find it's hard to test context switch because I don't know when a clock interrupt coming, where the two processes going...

So I want to know how Linux, Windows or other operating system test their process context switch?

1
Could you please post the code you use for context switching? - Abrixas2
@Abrixas2: Debugging the OP's code wasn't the question. The question is about testing / debugging methods. - Peter Cordes
If you're using an emulator like BOCHS or qemu that has a debugger or GDB-remote built-in, you can set a breakpoint in the scheduler code and single-step through it when it's called from an interrupt handler. (Or from a sched_yield system call if you want to trigger it synchronously.) - Peter Cordes
@PeterCordes This is a good way. And there is another question I want to know, is it possible to write unit test for operating system? If I add new feature to OS, I want to know weather it makes influence to whole system... - Jack

1 Answers

2
votes

Write a few tests in assembly language. The first one, which should run in an infinite loop, should set all of its registers to a known values, then test that the registers have those values. For example:

top:
    cmp rax_v(%rip), %rax
    jne rax_error
    cmp rbx_v(%rip), %rbx
    jne rbx_error
    ....
    jmp top

running this at a low priority should give you a pretty good coverage that interrupt handlers and interrupt -> context switch saves and restores the registers properly. Remember to use a wide variety of values in the registers to minimize false matches.

Following this pattern, make one which invokes system calls. Of course it cannot check all the registers, since the system call is free to use the ABI, but it can check the others. Also, if you intend your system to make guarantees about register leaking, you can check this here.

Run a set of the processes, maybe a few of them as threads too.

Next, you will want to increase your interrupt load, with the goal being to hit the maximum possible interrupt nesting. If you have real serial ports, they can be a great source of them -- hook up a noise source (function generator or antennae) to the Carrier Detect pin of the uart, and enable modem status interrupts. Most uarts do not gate these status pins on the baud clock, so you can get a maximal interrupt storm from this. Try and set the uarts to the lowest interrupt priority.

Good luck, you should have lots of fun ahead!