SystemCalls and Device Drivers Intro ------------------------------------ SysCalls What, Why, How Walkthru' fork(), exec(), exit( ) Device Drivers What, Why, How Main functions aka open(), read(), write(), close(), release() Demo Driver Walkthru' keyboard, mice and USB driver Booting Up(Optional) * Further Sections * MM * FS * USB Drivers * Process Details ? * Remember * Each class will be maximum of 1.5-2 hrs
Linux Scheduler
There is a doc within the documentation directory which explains nicely and clearly
about the major purpose of the scheduler.
The new things in the new scheduler are :-
1. Fully O(1) scheduling
2. Perfect 'SMP' scalability
No big "run_queue" lock. All per CPU runqueues and locks.
3. Better SMP Affinity
No random bouncing of processes between CPUs if/when there is a higher priority/interactive
task.
4. Batch Scheduling
5. Handles Extreme load smoothly
6. O(1) RT Scheduling
7. Run forked children before parent.
Scheduling Blues
Long time since I last wrote. Anyway I am polishing up the docs wrt scheduling which are present in the Documentation directory. Amazingly good read. Clears up a lot of grey areas.
Will be following up with about 400-500 lines of sched.c
Finally things are falling up at their rightful place.
sched.c
Kernel > sched.c Functions analysis
1. void __preempt_spin_lock(spinlock_t *lock)
This function is effective only when kernel is SMP supportive and preemption is enabled. Here, the calling process spins on the lock while permitting preemption.
Analysis:
If preemption is enabled, then create a Read-write spinlock, allowing multiple readers but only one writer.
Otherwise enable preemption, check whether the spinlock is still spinning and disable the preemption, incase it is . Do this till the spin lock exists.
[XFree86] Re: Framebuffer server : "Xvfb (virtual framebuffer server) is an X server that does not
use a video display. It uses system memory to simulate a video
card. It is useful for testing purposes, or for running
applications which require an X server but which you do not
really need a display and might not have video hardware. For
example, you can run realplayer in Xvfb to be able to play real
audio files from the commandline. There is no video display
though, it is just faked in system memory."
Ptrace Sys Call
Rough Draft
01/11/2004 06:58:13
DOUBT: Can a child be ptraces by more than 1 process??
1. First line of __ptrace_link() states that if a process is already being
ptraced, then it is a BUG() to trace it again.
Func: __ptrace_link()
Working: Makes debugging process the parent of the process which is being
debugged. Also tag child's ptrace list to parent's ptrace_children variable(??)
I think this is being done so that the parent might know which all children is
it debugging.
NOTE: There are 2 parents associated with a child - current parent and real
parent. A real parent is one which was forked to create this process. Current
parent is one which is currently pptracing this process. Both may not be the
same. They are definitely not same when the process is being ptraced.
Func: ptrace_attach
This is the core func. It is used to attach the passed process as the one which
is being tracked by the *current* process.
Func: access_process_...
Notes Ch4 Understanding the Linux Kernel
There is a key difference between interrupt handling and process switching: the code executed by an interrupt or by an exception handler is not a process. Rather, it is a kernel control path that runs on behalf of the same process that was running when the interrupt occurred . As a kernel control path, the interrupt handler is lighter than a process (it has less context and requires less time to set up or tear down).
The activities that the kernel needs to perform in response to an interrupt are thus divided into two parts: a top half that the kernel executes right away and a bottom half that is left for later.
Interrupt Types: Maskable interrupts : Sent to the INTR pin of the microprocessor. They can be disabled by clearing the IF
flag of the eflags register. All IRQs issued by I/O devices give rise to
maskable interrupts. Nonmaskable interrupts : Sent to the NMI (Nonmaskable Interrupts) pin of the microprocessor. They are not
...
Using ptrace
ptrace() is a system call that enables one process to control the execution of another. It also enables a process to change the core image of another process. The traced process behaves normally until a signal is caught. When that occurs the process enters stopped state and informs the tracing process by a wait() call. Then tracing process decides how the traced process should respond. The only exception is SIGKILL which surely kills the process.
The traced process may also enter the stopped state in response to some specific events during its course of execution. This happens only if the tracing process has set any event flags in the context of the traced process. The tracing process can even kill the traced one by setting the exit code of the traced process. After tracing, the tracer process may kill the traced one or leave to continue with its execution.
Prototype of ptrace is
#include
long int ptrace(enum __ptrace_request request, pid_t pid,
...
Here is a mini tute on how to use CVS
Starting
1. Set CVSROOT to the main repositoy where you want to store all the files.
Otherwise all commands have following format cvs -d /usr/local/cvs command
After setting CVSROOT, we can simple do : cvs command
2. To create a new project, go to the dir with the source and do
cvs import -m "log msg" projname vendortag releasetag
This imports all the files in this directory recursively into project. Our project is now a part of repository.
3. To get an actual working copy, we need to checkout the module.This is done as cvs checkout projname
You have a working copy of the code in CVS
Wait Queues
Wait queues are an implementation of lists. They are primarily used to provide synchronization.For example all the processes waiting for a particular event.
Wait queues are implemented in kernel in <linux/wait.h>
The 2 important structures are :-
The wait queue head structure
struct __wait_queue_head {
spinlock_t lock;
struct list_head task_list;
};
The wait queue structure
struct __wait_queue {
unsigned int flags;
#define WQ_FLAG_EXCLUSIVE 0x01
struct task_struct * task;
wait_queue_func_t func;
struct list_head task_list;
};
TODO: I need to find out what is the wait queue function responsible for?? Is it something like that this function will be executed
A wait queue event might be uninterruptible or interruptible. An interruptible event might interrupted as per a particular time interval. All these functions are available in <linux/wait.h>.
As an exampl...
Kernel Lists
To prevent duplicated efforts in creating a linked lists and to provide a common interface in sharing them, the kernel developers have declared a kernel level linked list.
The appropriate header is
Here is the most imp struct
struct list_head {
struct list_head *next, *prev;
};
Note that this list_head only mentions the next and prev pointers, it says nothing of the information. Therefore we need to embed this structue inside the structures that make up the list. So the actual struct will be defined as
struct task_list {
struct list_head list;
task_struct *task;
};
The head of he list must be a standalone list_head structure. It must be initialized to use with the INIT_LIST_HEAD macro. Ex
INIT_LIST_HEAD(&list);
Alternatively list-head can be initialized at compile time as follows :-
LIST_HEAD(list);
Now we can use functions in . SOme imp funcs are :-
1. list_add()
2. list_add_tail()
3. list_del()
4. list_empty()
5. list_splice()...
Fork Syscall
1. There is a function fork.c::nr_processes() which gets the number of processes. Note that processes corresponding to a CPU are included only
when the CPU is online. It gets these values using the per_cpu() macro which gets the values used by CPU.
2. These are the 2 macros defined for allocating and freeing a task struct
# define alloc_task_struct() kmem_cache_alloc(task_struct_cachep, GFP_KERNEL)
# define free_task_struct(tsk) \
kmem_cache_free(task_struct_cachep, (tsk))
3. Wait Queue Functions
The folowing functions deal with wait queues of processes
add_wait_queue
add_wait_queue_exclusive
remove_wait_queue
prepare_to_wait
prepare_to_wait_exclusive
finish_wait
autoremove_wake_function
All the functions dealing with wait_queues are simple wrapper on functions given in
< linux/wait.h > . Generally they change the wait queue flag and then do a spin_lock before the call and a spin_unlock() after the call.
Pls see the ...
How do syscalls work
A system call works on the basis of a defined transition from user mode to system mode. In Linux, this is possible through interrupts. Therefore the interrupt 0x80 is reserved for this only.
User calls a lib func. This func writes its args and number of system call to defined transfer regs and triggers 0x80 interrupt. When ISR returns, the value is read from appropriate transfer reg and the lib func returns.
Actual work of syscall is done by the interrupt routine. This starts a entry address system_call(), held in arch/i386/kernel/entry.S file.
Here we explain the broad level functioning of system_call
1. All regs for the process are saved.
2.