views:

27

answers:

1

I finished homework for a graduate course in operating systems. I got a great score and I only missed one tiny point of a question. It asked which were privileged instructions and which were not. I answered all correctly except one: Adding one register value to another

I answered it was privileged but apparently it's not! How can this be?

I figured the user interacts with registers/memory by using systems calls, which in a sense change from user mode system calls to kernel mode routines. Therefore the adding of one register value to another could be called by a non-privileged user, but in the end the kernel is doing the work and is in kernel, privileged mode. Therefore it's privileged? A user can't do it by themselves. Am I wrong? Why?!

  • Thanks!
+2  A: 

I'm not sure why you would think that changing a register would require kernel intervention. Some special registers may be privileged (those controlling things like descriptor tables or protection levels, with which user-mode code could bypass system-mode protections) but general purpose registers can be changed freely without a kernel getting involved.

When your code is running, the vast majority of instructions would be things like:

inc  %eax
movl $7,%ebx
addl %eax,%ebx

As an aside, I'm just imagining how slow my code would run if it required a system call to the kernel every time I incremented a counter or called a function :-)

The only thing I can think of would be if you thought your execution thread wasn't allowed to change registers arbitrarily since that may affect those registers for other threads. But the kernel would take care of that when switching threads - all your registers would be packed away somewhere for later and the ones for the next thread would be loaded in.


Based on your comments, you seem to think that the time of adding is when the CPU protection mechanism should step in. In fact, it can't at that point because it has no idea what you're going to use the register for. You may just be using it as a counter.

However, if you do use it as an address to access memory, and that memory is invalid somehow (outside of your address space, or swapped to disk), the kernel will step in at that point to rectify the situation (toss your application out on its ear, or bring in the swapped-out memory).

However, even that is not a privileged instruction, it's just the CPU handling page faults.

A privileged instruction is something that you're not allowed to do at all, like change the interrupt descriptor table location registers or deactivate interrupts.

paxdiablo
So, let me get this straight. There are special purpose registers which require privileged instructions, but general registers do not require privileged instructions. Does this mean if you add two addresses, it does so without any system calls? How does this happen?
Google
I did think there must be some kind of protection set up by the kernel, say if process A's register value tries to add to process B's register value. I guess that would result in a trap and the routine handler would take care of it?
Google
You add them. That's it really. You execute an instruction like `add %eax,%ebx`. The CPU takes the value in `%eax` and adds that to `%ebx`. The protection on addresses is done at dereferencing time. If you use that modified `%ebx` to try and access memory outside of your address space, _that's_ the time at which the protection mechanism will step in. Up until then, the CPU doesn't know that you're going to use `%ebx` to access memory. Clarifying in answer.
paxdiablo
Thanks, this all makes perfect sense. Thanks a lot!
Google