Kernel Services - PowerPoint PPT Presentation

1 / 26
About This Presentation
Title:

Kernel Services

Description:

increment the time of day. check for an interval timer on the currently running process ... monitors some peripherals that require polling. process scheduling ... – PowerPoint PPT presentation

Number of Views:81
Avg rating:3.0/5.0
Slides: 27
Provided by: SteveC133
Category:
Tags: kernel | services

less

Transcript and Presenter's Notes

Title: Kernel Services


1
Kernel Services
  • CIS 657

2
System Processes
  • Three processes created at startup time
  • init
  • process 1
  • user-mode
  • administrative tasks (keeps getty running
    shutdown)
  • ancestor of all of your processes
  • swapper
  • process 0
  • kernel process
  • moves entire processes between main memory and
    secondary storage
  • pagedaemon
  • process 2
  • kernel process
  • moves parts of processes in and out

3
Note on Init and Logging In
  • init keeps a getty process running on each
    terminal port (tty)
  • getty initializes the port and waits for a login
    name (the login prompt)
  • getty reads a string and execs login
  • login prompts for the password, performs one-way
    encryption, and compares values
  • if successful, login sets the user id and execs a
    shell

4
Run-Time Organization
  • Top Half
  • per-process stack
  • library of shared code
  • maintains process structure (always resident)
  • maintains user structure (can be swapped out)
  • division between process/user dependent on memory
  • never preempted for another process (but can
    yield the processor)
  • can block interrupts by setting processor
    priority level (see discussion on bottom half)

5
Run-Time Organization II
  • Bottom Half
  • handles hardware interrupts
  • asynchronous activities
  • special kernel stack (might not be a process
    running)
  • bottom half runs at specific priority level
  • top half can block specific devices from
    interrupting by setting priority level before it
    modifies work queues
  • top half starts I/O requests, waits for bottom
    half to finish

6
Entry Into the Kernel
  • Hardware interrupt
  • I/O device (disks, network cards, etc.)
  • clock (used for scheduling, time of day)
  • Hardware trap
  • divide by 0, illegal memory reference
  • Software-initiated trap
  • system call

7
Entry Into the Kernel II
  • First, kernel must save machine state
  • Why?
  • Example sequence
  • hardware switches to kernel mode
  • hardware pushes onto per-process kernel stack the
    PC, PSW, trap info
  • additional asm routine saves all other state that
    the hardware doesnt
  • kernel calls a C routine--the handler.

8
Entry Into the Kernel III
  • Handlers for each kind of entry
  • syscall() for a system call
  • trap() for hardware traps
  • interrupt handlers for devices
  • Each kind of handler takes specific parameters
    (e.g., syscall number, exception frame or the
    unit number for an interrupt).

9
Return From Kernel
  • asm routine restores registers and user stack
    pointer (it undoes what the companion asm routine
    did)
  • hardware restores the stored PC, PSW, etc.
    (undoes what it did on the way in)
  • execution returns at the next instruction in the
    user process

10
Software Interrupts
  • Used as low-priority processing mechanism in the
    kernel
  • Hardware interrupts have high priority
  • Can put work in work queues (cf. network)
  • When high-priority work is done, low-priority
    software interrupt does the rest
  • might be real interrupt, might be flag checked in
    kernel (architecture-dependent)
  • can be preempted by another hardware interrupt

11
What FreeBSD Does for x86
  • The cpl variable holds the current priority level
  • Various macros are defined to set the cpl to a
    new level (e.g. spl0(), splx(), spltty())
  • Interrupts at lower levels are masked (but not
    really--a pending bit is set and the majority of
    the work handling the interrupt is deferred)
  • See /usr/src/sys/i386/isa/ipl_funcs.c

12
Clock Interrupts
  • The system clock interrupts, or ticks, at regular
    intervals (usually 100 Hz).
  • Interrupt handler calls the hardclock() routine,
    which must run quickly
  • running for more than one tick will miss the next
    interrupt, causing the time-of-day clock to skew
  • lower-priority devices (network devices, disk
    controllers) cannot be serviced while hardclock()
    is running.
  • Non-critical clock functions handled by
    softclock()

13
Clock Interrupts II
  • What hardclock() does
  • increment the time of day
  • check for an interval timer on the currently
    running process
  • do the job of statclock() if there is no separate
    clock for statistics gathering
  • call softclock() directly if the cpl is low
    (saves overhead of a software interrupt that
    would just do that when hardclock() returns)
  • See /usr/src/sys/kern/kern_clock.c

14
Statistics
  • Hardclock() used to collect resource utilization
    statistics, and forced context switches
  • Problems (see McCanne Torek)
  • potential for inaccurate measurement of CPU
    utilization
  • inaccurate profiling
  • Use semi-randomized sampling with a second clock
    (the stat clock, see statclock())
  • charge the current process with a tick if it has
    four, recalculate priority
  • record what the system was doing at time of tick

15
Softclock()
  • Handles events in the callout queue, such as
  • timeouts (real-time timer)
  • retransmits dropped network packets
  • monitors some peripherals that require polling
  • process scheduling
  • Scheduler would/should be called every second in
    a perfect world uses interval timer to run 1
    second after it last finished
  • Discussion why not do scheduling in hardclock()?

16
Callout Queue
  • Sorted in time order (soonest first)
  • Delta queue
  • hardclock() decrements first entry, and
    softclock() only runs when that reaches 0.

queue
1 tick
3 ticks
0 ticks
81 ticks
time
function argument
f(x)
g(y)
f(z)
h(a)
10 ms
40 ms
40 ms
850 ms
when
17
Memory Management
  • Two kinds of executable files in BSD Unix
  • interpreted
  • compiled (directly executed)
  • First 16 bits in a file contain a magic number
    telling what kind of file it is.
  • ! indicates an interpreted file interpreter
    must be directly executable (!/bin/sh is the
    most common)
  • Other magic numbers indicate whether the file can
    be paged and whether the text is sharable.

18
UNIX Process Layout
0xfff00000
  • 0-filled bss, stack
  • most of process is demand paged into memory (Ch.
    5)

Symbol table
Initialized data
text
0x00000000
a.out header
magic number
19
What Was That Special Stuff?
Per-process kernel stack
  • Argv, argc, envp contain arguments and
    environment
  • signal code used by kernel to deliver signals
  • ps_strings used by ps to located argv of process
  • user (u.) area (swappable context)
  • red zone is read-only page (not on all arch)

Red zone
User area
Ps_strings struct
Signal code
Env strings
argv strings
Env pointers
argc
argv pointers
20
Timing Services
  • Real Time gettimeofday() returns the time since
    1 Jan 1970 in UTC (the Epoch)
  • adjtime() allows one to tweak the clock
  • keeps multiple machines close enough
  • response to normal clock skew
  • give a delta argument speed up or slow down the
    counted microseconds per clock tick by 10 until
    delta is reached
  • Time is reported in microseconds

21
Interval Timers
  • Each process gets three interval timers
  • real decrements in real time SIGALRM run from
    timeout queue maintained by softclock()
  • profiling decrements only when process runs, but
    tracks both user and kernel-mode execution
    SIGPROF checked by hardclock()
  • process virtual decrements only when the process
    is running SIGVTALRM checked by hardclock()

22
User, Group, Other Identifiers
  • User ID (uid) 32-bit identifier for all
    processes of each user, set by administrator
  • Group ID (gid) 32-bit identifier. Many users in
    one group many groups for each user
  • Root uid 0, gid 0.
  • These bits are checked on file access

23
Permission Checks
  • Checked in order
  • If the UIDF UIDP use owner permission bits
  • If UIDF ! UIDP, but GIDF GIDP then use group
    permission bits
  • If UIDF ! UIDP, and GIDF ! GIDP then use the
    other permission bits
  • Recall discussion last time of how uid, gid set
    on login
  • Can I own a file that I cant read?

24
Rights Amplification
  • Users may need temporary write access on files
    (e.g. passwd)
  • setuid() does this
  • changes effective user id
  • real user id stays the same
  • effective uid also saved
  • seteuid() changes only the effective user id
  • setgid()
  • used to work like setuid()
  • now just put effective gid into 0th element of
    array

25
Effects of Syscalls on UIDs
Action
Real
Effective
Saved
Exec-normal
R
R
R
Exec-setuid
S
R
S
Seteuid(R)
R
R
S
Seteuid(S)
R
S
S
Seteuid(R)
R
R
S
Exec-normal
R
R
R
R Real UID, S Special-privilege UID
26
Homework Assignment for FreeBSD
  • In which file is each of the following defined?
  • hardclock()
  • softclock()
  • statclock()
  • In which file or files is the code that checks
    the current interrupt priority level and handles
    soft interrupts?
  • Read the McCanne Torek paper, and write an at
    most three paragraph summary of it.
  • Due 26 Sep 2001, by e-mail to cis657ta alias
Write a Comment
User Comments (0)
About PowerShow.com