difference between Preemption and context switch
Asked Answered
T

3

8

A little intro,

I am currently writing a small (read tiny) RTOS kernel, well it's supposed to be monolithic with most stuff in the kernel. However I can't find much information on a few things listed below, It would be a lot helpful and besides this, it isn't actually some kind of university project but something I'm doing at my own will.

A better alternative to answering all the questions would be if you could refer to me a freely available RTOS (or even a free book) for arm preferably which implement userspace and are preemptible (but not complex like linux). Linux has some of the worst documentation I've seen till now (I did try figuring things out from linux code but there are just a tons of defines scattered through a million files and function hooks with wierd names and stuff getting renamed every version also getting moved sometimes...)

  1. What is the difference between "preemption" and "context switch" ?

  2. What are the key differences between a preemptive and nonpreemptive kernel ? What all work is required from a programmer to make the kernel preemptive ?

  3. How to create and work with user mode ?

    ARM docs say that in user mode, any instruction switching to a privileged mode will be treated as undefined instruction.

  4. If so, the only way for a userspace program to use kernel code is syscalls ?

  5. How does a kernel respond or interact with userspace then ?

  6. Does that mean the only kernel thread after booting (in a simple system) would be the idle thread ?

  7. If the Page where kernel code and data resides is unmapped when switching to a user process, then on a syscall or interrupt, how does the kernel code execute without being mapped in the virtual address space ?

  8. Does a 'preemptible kernel' only mean that the kernel was designed in a way that it would be safe to have context switch during execution of kernel code ? or does it require more work to be done if any ?

Oh and if such multiple questions are not allowed here, sorry, couldn't find anything about that.

Tshirt answered 22/7, 2012 at 17:23 Comment(6)
From the faq: "Your questions should be reasonably scoped. If you can imagine an entire book that answers your question, you’re asking too much." There are too many questions here, and the scope is really broad. Looks like you need a good book on operating system design in general, and a good one on ARM in particular.Quiles
Yep, A book would be a good thing as mentioned in the question. the scope itself of the questions are not so broad, they're either about preemption or the relation of kernel-user. However I'm afraid that they are too many for a single post, sorry for that. I'll see what a mod recommends to either break this down or change it to something like "need a good system design book for arm"..Tshirt
Book recommendation questions don't work here. (There are a few historical ones that stick around, but new ones are not welcome.)Quiles
I'd be happy to have my answers in any form be it a link to a thread as old as myself, a link to a book, a long answer here, a link to source code of some good (free) software etc.. However, if i do need recommendation for a good system design book(though i was just looking for a few answers, a good read won't hurt), where do i post it ? this was the most relevant stack site i could think of.Tshirt
Stack Exchange sites in general are Q&A, not recommendation engines.Quiles
See: #5284001 , #3544159 , then ask any remaining questions you have in a separate post for each question.Griggs
M
16

As Mat wrote, this is probably unreasonably scoped. However, I'll try to devote as much attention to the sum of the questions as I would to one reasonably scoped question, in the hopes that this will help you to begin researching.

1 What is the difference between "preemption" and "context switch" ?

Preemption is the act of interrupting a process without its involvement. In this context, that probably means a timer interrupt will fire. The word comes from a legal concept of preemption: the act or right of claiming or purchasing before or in preference to others. For your purposes, that means that when the timer interrupt fires, that the interrupt service routine (ISR) has preference over the code which was previously running. This doesn't necessarily need to involve a kernel at all; you can have code running in any ISR which will run preemptively.

A context switch is what happens when the OS code (running preemptively) alters the state of the processor (the registers, mode, and stack) between one process or thread's context and another. The state of the processor may be at a certain line of code in a one thread. It will have temporary data in registers, a stack pointer at a certain region of memory, and other state information. A preemptive OS can store this state (either to static memory or onto the processes' stack) and load the state of a previous process. This is known as a context switch.

2 What are the key differences between a preemptive and nonpreemptive kernel ? What all work is required from a programmer to make the kernel preemptive ?

In a preemptive kernel, an interrupt can fire in between any two assembly instructions (known as 'sequence points'). In a non-preemptive kernel, the running process must call a yield() function to allow the other threads to run. Preemptive kernels are more complex, but provide a better illusion of concurrency. Non-premptive kernels can be done very simply with setjmp.h, but each thread must regularly call yield() or the other threads will not run.

When a function like yield() is called, the state of the processor is stored automatically. When you want to make your OS preemptive, you must store this information manually.

3 How to create and work with user mode ?

ARM docs say that in user mode, any instruction switching to a privileged mode will be treated as undefined instruction.

Correct. However, they also say that any interrupt will run in privileged mode automatically. On an ARM system, you can use the svc instruction to generate a software interrupt. The SVC code (part of your OS) will then be able to run in privileged mode.

4 If so, the only way for a userspace program to use kernel code is syscalls ?

Correct. At least, this is the only safe or correct way.

5 How does a kernel respond or interact with userspace then ?

On ARM, the SVC instruction can get an 8-bit value. This can be used to generate 256 syscalls such as yield, enable interrupts, disable interrupts, or whatever you need. You may also choose to create a shared memory or message passing interaction mechanism if you need that.

6 Does that mean the only kernel thread after booting (in a simple system) would be the idle thread ?

That depends entirely on how you design your system. It's probably simpler if you choose to start your kernel only after all your threads have been created - that way you don't need to worry about dynamically allocating threads. Or, you can start with the idle thread and add other threads later (through a remote shell? I think you'd want at least one user thread running consistently...)

7 If the Page where kernel code and data resides is unmapped when switching to a user process, then on a syscall or interrupt, how does the kernel code execute without being mapped in the virtual address space ?

Just as kernel mode code runs in privileged mode even if the code was previously executing in user mode, so will kernel mode code run from the main stack pointer (MSP) even if the process code was using a different address space.

8 Does a 'preemptible kernel' only mean that the kernel was designed in a way that it would be safe to have context switch during execution of kernel code ? or does it require more work to be done if any ?

I think that means that the kernel can preempt the user code, not that the kernel itself can be preempted. It would be difficult and unusual for anything to interrupt the kernel. That would require more work, and I'm struggling to see why you'd want it.

Mathi answered 22/7, 2012 at 17:54 Comment(9)
Kernel preemption is really important (I'd wager especially for real-time systems). linuxjournal.com/article/5600Quiles
satisfying answer however I still don't understand In 7, I asked that if kernel code is not mapped in virtual space, how does cpu fetch code from there? shouldn't that cause a data abort? In 8, Since this is a monolithic kernel I am implementing "most" stuff in the kernel so that would require me to actually make the kernel code pre emtive, suppose i get a spinlock acquire syscall or some kernel thread calls it on a single CPU, would lead to a deadlock but if that thread was preempted, it would run fine on any system since the thread would be ejected after using up all of it's quantum.Tshirt
@user1075375 - 7 - No, that won't cause a data abort because the CPU is running in privileged mode. At least, that's how I understand it - I admit that most of my work has been done on systems without an MMU or virtual memory. Re 8 - If you have multiple CPUs, then yeah, it would be useful if the kernel code was preemptible. However, yu can't get a spinlock acquire syscall from the current CPU - when kernel code is running, user code is not running.Mathi
@Quiles - Ah, I see - you're basically running another kernel concurrently with your preemptible kernel. Interesting!Mathi
I've upvoted it already However i will wait for a better answer if any for some more time, thanks for your time.Tshirt
Generally, an excellent article. I have only two minor issues. 1. A context switch can happen in non pre-emptive kernels too. 2. In cooperative multitasking, all system calls generally contain an implicit yield(). So processes only need to explicitly yield during long sections of code with no sys calls in them.Trilinear
@user1075375 you aren't going to get a better answer with this one already posted. Other people like me could have posted answers but we're not going to because most of their text would be repetition of this one.Trilinear
@Trilinear - (1) True. (2) That's news to me! Makes some sense, though. Code I'd been working with required explicit yields at the desired points which gave a little more control over the state, but I can see how that could be useful.Mathi
@KevinVermeer If you think about it, it has to be the case. If a process requests a block from the disk, you don't want the whole computer to stop while it is being retrieved. You might as well switch in another process. Having said that, explicit yields are often required as well.Trilinear
P
1

Rather than answering each of your enumerated questions, I'll do my best to service your (thankfully) bolded request:

A better alternative to answering all the questions would be if you could refer to me a freely available RTOS (or even a free book) for arm preferably

Micrium's uC/OS-III is a priority-based real-time kernel that (of course) supports both synchronous and asynchronous preemption. And, as luck would have it (and the reason I'm replying) is that there is a free book available, and also the source code is available.

Head over to the main page for uC/OS-III and on the left you'll see a link for a video talking about source code availability ("uC/OS-III Source is available").

As far as books go, go over to the projects page, and choose the book that most closely matches your target. 90% of the material is the same; only the CPU-specific stuff (like context switching, interrupts & initialization) will vary from book to book.

You'll have to register to download the book & the code, seems fair to me.

Good luck and have fun. Thanks for putting in bold your ultimate request / goal, that made this much easier.

Passus answered 24/7, 2012 at 3:11 Comment(1)
Thanks, will look into that too.Tshirt
M
1

I was under the impression that the uC/OS-III was not free, but licensed.

A great free RTOS, and very well explained, is the FreeRTOS (http://www.freertos.org/)

You should definitely take a look there

Maul answered 24/7, 2012 at 5:20 Comment(5)
uC/OS isn't free to license in commercial products and hasn't ever been. Pricing (from what I've heard) for the various product/product line/cpu/site licenses ranges from $10k to $20k. However, it's always been source-available and the book is fantastic (yes, I think it's better documented than FreeRTOS); Jean Labrosse is great about education and openness.Mathi
The suggested edit has been rejected: stackoverflow.com/suggested-edits/327885Mathi
@KevinVermeer - That's a pretty extreme edit, and it does change the meaning of the answer, so I can see why people rejected it. My suggestion would be to either post it as a completely new answer or append it to your accepted answer. It looks like great material, but we do try to respect the intentions of the original author.Pseudohermaphrodite
@BradLarson - Thanks for weighing in. I realize that it adds a lot, but I'm not adding new information. I'm clarifying the author's intent: Backing up and clarifying the vague 'impression' with evidence from the source site, changing from a plain-text link to properly-formatted links to useful portions of the large site, and quoting a relevant article about the merits of the suggested RTOS.Mathi
@KevinVermeer - There's a subtle change in tone at the beginning, and you do add a lot of new information. We've had people complain in the past about editors changing too much about their answer with additions like this, so I still think the best solution would be to write an answer of your own based on your edit (maybe saying something like "to expand upon what Rafael said in his answer..."). There's no problem with having multiple answers to a question.Pseudohermaphrodite

© 2022 - 2024 — McMap. All rights reserved.