User and Kernel Mode, Interrupts, and System Calls Flashcards

1
Q

What happens when the cpu runs in Kernel mode?

A

● It can run any instruction in the CPU
● It can modify any location in memory
● It can access and modify any register in the CPU and
any device.
● There is full control of the computer.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

What happens when the cpu runs in User mode?

A

●The CPU can only use a limited set of instructions
● The CPU can only modify only the sections of memory
assigned to the process running the program.
● The CPU can access only a subset of registers in the CPU
and it cannot access registers in devices.
● There is a limited access to the resources of the computer.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

What services run in Kernal mode, which run in user mode?

A

OS services run in Kernel mode, user programs run in user mode

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

Does the computer boot up in Kernel or User mode?

A

● When the OS boots, it starts in kernel mode.
● In kernel mode the OS sets up the interrupt vector
and initializes all the devices, and kernel data
structures.
● Then it starts the first process and switches to user
mode.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

True or False, programs runs in Kernel mode.

A

False, programs run in user mode

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

What is an interrupt?

A

An event that requires immediate
attention. In hardware, a device sets the interrupt line to high.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

When do user programs switch to kernel node?

A

To request OS services (system calls). When an interrupt arrives.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

Is most of the cpu time spent in Kernel or
User mode?

A

User mode

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

Are interrupts executed and (interrupt vectors) modified in Kernel or User mode?

A

Kernel mode

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

What are some reasons we separate User and Kernel mode?

A

Separation of user/kernel mode is used for:
● Security: The OS calls in kernel mode make sure that the
user has enough privileges to run that call.
● Robustness: If a process in user mode tries to write to an
invalid memory location, the OS will kill the process, but the
OS continues to run. A crash in the process will not crash
the OS. > A bug in user mode causes program to crash,
OS runs. A bug in kernel mode may cause OS and system
to crash.
● Fairness: OS calls in kernel mode to enforce fair access.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

What is an interrupt?

A

An event that requires immediate
attention. In hardware, a device sets the interrupt line to high.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q

After the executing the interrupt handler what happens?

A

The CPU returns to place it was at before interrupt and the program continues.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
13
Q

When an interrupt happens, where does the cpu ‘jump’ to?

A

The cpu jumps to the interrupt handler to handle the specific interrupt.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
14
Q

What are some examples of interrupts?

A

Moving the mouse, typing a key, an ethernet packet

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
15
Q

What are the specific steps of serving an interrupt?

A
  1. The CPU saves the Program Counter and registers
    in execution stack
  2. CPU looks up the corresponding interrupt handler
    in the interrupt vector.
  3. CPU jumps to interrupt handler and run it.
  4. CPU restores the registers and return back to the
    place in the program that was interrupted. The
    program continues execution as if nothing
    happened.
  5. In some cases it retries the instruction that was
    interrupted (E.g. Virtual memory page fault
    handlers).
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
16
Q

How do interrupts allow the CPU and device to run In parallel without waiting for each other?

A

Device runs operations then ‘interrupts’ the OS. Then OS continues post interrupt.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
17
Q

What is polling?

A

The process instead of using interrupts where the OS waits in a busy loop until completion.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
18
Q

Is polling efficient?

A

No it wastes a lot of cpu cycles.

19
Q

When was polling used?

A

In early PCs when devices were simple, its also still used in some microcontrollers.

ex: used to print debug messages in the kernel.

20
Q

What are Synchronous and Asynchronous Processing

A

Synchronous refers to Polling because execution of device is “synced” with the program

Asynchronous processing refers to interrupts because device and program execution are not in sync. The device and CPU run in parallel.

21
Q

What is an interrupt vector?

A

It is an array of pointer to functions that point to the different interrupt handlers of the different types of interrupts.

22
Q

What are some of the different interrupt handlers?

A

Hard Drive IH, USB IH, Ethernet Card IH, Page Fault IH

23
Q

Why do interrupts run in kernel mode?

A

An interrupt handler must read device/CPU registers and execute instructions only available in kernel mode.

24
Q

Can an interrupt vector be modified in user mode? why?

A

No, only in kernel mode for security reasons

25
Q

When is an interrupt vector initialized and modified?

A

It’s initialized on boot up, and it’s modified when drivers are added to the system.

26
Q

What are the 4 types of interrupts mentioned on the slides?

A
  1. Device interrupts
  2. Math exception
  3. Page fault
  4. Software Interrupt
27
Q

What is a device interrupt and what are some examples?

A

Device Interrupts generated by Devices
when a request is complete or an event that requires CPU attention happens.
Ex:
● The mouse is moved
● A key is typed
● A WiFi/Ethernet packet arrives.
● The hard drive/solid state drive has completed a
read/write operation.
● A CD has been inserted in the CD drive.

28
Q

What is a math exception and what are some examples?

A

Math exception is a type of interrupt generated by the CPU when
there is a math error
Ex:
● Divide by zero

29
Q

What is a page fault and what are some examples?

A

Page fault is a type of interrupt generated by the MMU (Memory Management Unit) that converts Virtual memory addresses to physical memory addresses
Ex:
● Invalid address: interrupt prompts a SEGV signal to the
process
● Page not resident. Access to a valid address but there is
not page in memory. This causes the CPU to load the
page from disk
● Invalid permission (I.e. trying to write on a read only
page) causes a SEGV signal to the process.

30
Q

What is a software interrupt?

A

Software Interrupt generated by software
with a special assembly instruction
(SYSCALL, INT, TRAP, TA). This is how a
program running in user mode requests
operating systems services.

31
Q

What is a system call?

A

System Calls is the way user programs request services from the OS.

32
Q

What do system calls use to request services?

A

System calls use Software Interrupts.

33
Q

What are some examples of system calls?

A

● int open(filename, mode)
● read(file, buffer, size)
● write(file, buffer, size)
● int fork()
● execve(cmd, args);

34
Q

Why are system calls like an API?

A

System calls is the API of the OS from the user program’s point
of view.

35
Q

Why do we use Software
Interrupts for syscalls instead of
function calls?

A

● Software Interrupts will switch into kernel
mode
● OS services need to run in kernel mode
because:
● They need privileged instructions
● Accessing devices and kernel data structures
● They need to enforce the security in kernel mode.

36
Q

What is the only type of operation that is part of system calls.

A

Only operations that need to be executed by the OS in kernel mode are part of the system calls

37
Q

Are functions sin(x), cos(x) system calls?

A

No, they don’t need to be executed by the OS

38
Q

What are two examples of functions that run almost fully in user mode, but then require system calls.

A

printf(s) runs mainly in user mode
but eventually call write() when for example the buffer is full and needs to be flushed.

Also malloc(size) will run mostly in user mode but
eventually it will call sbrk() to extend the heap.

39
Q

What does libc do in correlation with system calls?

A

Libc (the C library) provides wrappers for the system calls that eventually generate the system calls.

40
Q

What are some rolls of the interrupt handler?

A

The software interrupt handler for system
calls has entries for all system calls.
● The handler checks that the arguments are
valid and that the operation can be executed.
● The arguments of the syscall are checked to
enforce the security and protections.

41
Q

What is checked by the interrupt handler for: open(filename, mode)

A

● If file does not exist return error
● If permissions of file do not agree with the mode the file
will be opened, return error. Consider also who the owner
of the file is and the owner of the process calling open.
● If all checks pass, open file and return file handler.

42
Q

What happens when an error in a system call occurs?

A

The OS sets a global variable called “errno” defined in libc.so with the number of the error that gives the reason for failure.

43
Q

How can you print the error message in a system call?

A

perror(s); where s is a string prepended to the message

44
Q

Walk through a systemically/interupt example starting with write(fd, buff,
n).

A
  1. The user program calls the write(fd, buff, n) system call to write to disk.
  2. The write wrapper in libc generates a software interrupt for the system call.
  3. The OS in the interrupt handler checks the arguments. It verifies that fd is a file descriptor for a file opened in write mode. And also that [buff, buff+n] is a valid memory range. If any of the checks fail write return -1 and sets errno to the
    error value.
  4. The OS tells the hard drive to write the buffer in [buff, buff+n] to disk to the file specified by fd.
  5. The OS puts the current process in wait state until the disk operation is complete. Meanwhile, the OS switches to another process.
  6. The Disk completes the write operation and generates an interrupt.
  7. The interrupt handler puts the process calling write into ready state so this process will be scheduled by the OS in the next chance.