Re: linux context switching

From: Kasper Dupont (kasperd_at_daimi.au.dk)
Date: 02/09/04


Date: Mon, 09 Feb 2004 11:25:50 +0100


"惯迭技快扁" wrote:
>
> In linux, is there kernel context?
> Assume following sistuation,
> 1. a user process is having CPU
> 2. the process calls systems calls such as ioctl()
> 3. the process is excuted in a kernel mode, at this momnet is there context
> switching?

I guess that depends on your exact definition of a
context switch. The privelege level is changed,
but that is mostly about just changing a few
segment registers. The page tables are not changed
(unless you are using the 4:4 patch). But of course
all registers are saved on the stack.

> 4. H/W interrupt occurs
> 5. save contexts of the process in a kernel mode
> 6. interrupt handler is excuted
> 7. the handler gets finished

Yes that is correct.

> 8. RET is called

No, a special instruction is used to return from
an interrupt. On ia32 the instruction is called
IRET. It does a litle more than just a RET
instruction.

> 9. at this moment, without preemption, the interrupted process in a kernel
> mode will be selected again.

Correct.

> If the steps I mentioned above is right, I think there is no pure kernel
> context to be saved.

All registers must be saved on the stack when the
handler is executed. This means that there will
in fact be two instances of all registers on the
stack. One instance of registers from the user
mode process at the point of entry to the kernel.
And one instance of registers from the process
while executing kernel code at the time of the
interrupt. And whether you call it a context
switch again depends on your definition. We are
switching from a process context to an interrupt
context. But no pages or priveleges are changed.

> All contexts have corresponding user processes. Am I right?

Not exactly. Some processes doesn't have a user
space. Those are the kernel threads like for
example: ksoftirqd, events, kblockd, kapmd,
pdflush, kswapd, aio, pagebufd, kseriod.

> Who owns Task number 0?

Task zero is special. It exists already before
the kernel is booted, and it will eventually
become the idle process, that just sits in a
loop and halts the CPU when nobody else wants
to use it. In an SMP system there will be more
than one task number zero.

> kernel?

Yes.

>
> I have analyzed source codes. But it's very hard to understand them and
> figure out my questions.
>
> Your sincere advices will be appreciated :-)

-- 
Kasper Dupont -- der bruger for meget tid paa usenet.
For sending spam use mailto:aaarep@daimi.au.dk
/* Would you like fries with that? */


Relevant Pages

  • Re: Questions regarding Synchronisation in Windows (Spinlocks)
    ... Vista/Longhorn now only ship multiprocessor kernel. ... Interrupts are handled in context of ISR. ... Since Spinlocks execute at DPC/Dispatch level, there is no way for the ... But an interrupt occurs which has an IRQL> DPC Dispatch level OR the ...
    (microsoft.public.win32.programmer.kernel)
  • Re: Why cant we sleep in an ISR?
    ... interrupt is not associated with any context in the first place. ... includes the kernel and userspace stack pointers, the register set, ... this, an interrupt, depending on the version of your kernel and arch, ...
    (Linux-Kernel)
  • Re: [patch 00/11] ANNOUNCE: "Syslets", generic asynchronous system call support
    ... properly separating the user execution context from the kernel execution ... What about TLS related kernel ...
    (Linux-Kernel)
  • Re: [PATCH 0 of 4] Generic AIO by scheduling stacks
    ... thought we couldn't do native kernel threads for "normal" threading ... disciplines in question: i hacked on scheduling, 1:1 threading, on Tux, ... the programmer from the risks and complexities of thread programming, ... Having a 1:1 relationship between user-space and kernel-space context is ...
    (Linux-Kernel)
  • Re: 2.4 in-kernel file opening
    ... > There is no way to open files within the kernel. ... It doesn't have a context. ... mmap() is actually what i wanted to do automatically without the need to ... things within the driver before the mmap- I guess that should be ...
    (Linux-Kernel)