Docsity

Kernel’s Task List - Advanced System Programming - Lecture Slides, Slides of Computer Applications

Partial preview of the text

Download Kernel’s Task List - Advanced System Programming - Lecture Slides and more Slides Computer Applications in PDF only on Docsity!

The kernel’s task list

Introduction to process descriptors and their related data-structures for Linux kernel version 2.6.

Multi-tasking

  • Modern operating systems allow multiple users to share a computer’s resources
  • Users are allowed to run multiple tasks
  • The OS kernel must protect each task from interference by other tasks, while allowing every task to take its turn using some of the processor’s available time

What’s on a program’s stack?

Upon entering ‘main()’:

  • A program’s exit-address is on user stack
  • Command-line arguments on user stack
  • Environment variables are on user stack

During execution of ‘main()’:

  • Function parameters and return-addresses
  • Storage locations for ‘automatic’ variables

Entering the kernel…

A user process enters ‘kernel-mode’:

  • when it decides to execute a system-call
  • when it is ‘interrupted’ (e.g. by the timer)
  • when ‘exceptions’ occur (e.g. divide by 0)

What’s on the kernel stack?

Upon entering kernel-mode:

  • task’s registers are saved on kernel stack

(e.g., address of task’s user-mode stack)

During execution of kernel functions:

  • Function parameters and return-addresses
  • Storage locations for ‘automatic’ variables

Supporting structures

  • So every task, in addition to having its own code and data, will also have a stack-area that is located in user-space, plus another stack-area that is located in kernel-space
  • Each task also has a process-descriptor which is accessible only in kernel-space

The Linux process descriptor

task_struct *stack^ state flags *mm exit_code *user pid *files *parent

mm_struct *pgd

pagedir[]

user_struct

signal_struct *signal

files_struct

Each process descriptor contains many fields and some are pointers to other kernel structures which may themselves include fields that point to structures

Something new in 2.

  • Linux uses part of a task’s kernel-stack

page-frame to store ‘thread information’

  • The thread-info includes a pointer to the task’s process-descriptor data-structure

Task’s kernel-stack

Task’s thread-info page-frame aligned

Task’s process-descriptor

struct task_struct (^) 8-KB

Fields in a process-descriptor

struct task_struct { volatile long state; void *stack; unsigned long flags; struct mm_struct *mm; struct thread_struct thread; pid_t pid; char comm[16]; / plus many other fields */ };

Finding a task’s ‘thread-info’

  • During a task’s execution in kernel-mode, it’s very quick to find that task’s thread-info object
  • Just use two assembly-language instructions:

movl $0xFFFFF000, %eax andl %esp, %eax

Ok, now %eax = the thread-info’s base-address There’s a macro that implements this computation

The kernel’s ‘task-list’

  • Kernel keeps a list of process descriptors
  • A ‘doubly-linked’ circular list is used
  • The ‘init_task’ serves as a fixed header
  • Other tasks inserted/deleted dynamically
  • Tasks have forward & backward pointers, implemented as fields in the ‘tasks’ field
  • To go forward: task = next_task( task );
  • To go backward: task = prev_task( task );

Doubly-linked circular list

init_task (pid=0)

newest … task

next_task

prev_task

Maybe a big /proc file…

  • We can’t know ahead of time how many tasks are active in our system – this will depend on many varying factors, such as who else is logged in, which commands have been issued, whether we’re using text-mode console or graphical desktop
  • So it’s perfectly possible our pseudo-file might ‘overflow’ its kernel-supplied buffer!

How to avoid buffer-overflow

  • Our module’s ‘get_info()’ callback-function has four parameter-values passed to it by the kernel: - char *buf - address of a small kernel buffer - char **start - address of a pointer variable - off_t offset - current offset of file-pointer - int buflen - size of the kernel buffer
  • The initial conditions are: offset == 0 and *start == NULL
  • Kernel’s behavior will vary if we modify *start