Fundamentals of Operating Systems Explained

 
1
 
CMSC421: Principles of Operating Systems
 
Nilanjan Banerjee
 
Principles of Operating Systems
 
Assistant Professor, University of Maryland
Baltimore County
nilanb@umbc.edu
http://www.csee.umbc.edu/~nilanb/teaching/421/
 
2
 
Announcements
 
Project 0 and Homework 1 out
Discussion
 grades will not be on Blackboard
Readings from Silberchatz
Optional
 but important
 
3
 
Discussion 1
 
Sign bit
 
Mantissa
 
exponent
 
1000
 
1000
 
1000
 
Integer/2s compliment
 
*(int *)&a
4
Kernel-userspace interaction
Application
  kernel
 
Hardware interrupts
 
software interrupts
    system calls
 
timers
 
exceptions
 
Timers/return from sys call
 
A closer look at system calls
5
Lets take an example
OS
_start
main:
 
libc
.c file
.s file
.o file
Exec.
 
assembler
 
libraries
 
linker
 
6
 
X86 assembly for system calls (older mechanism)
 
mov
 $1, %eax
 
mov
 $25, %ebx
 
int
  $0x80
 
Software interrupt
 
Jump to an address in the kernel
 where the syscall table is stored
And execute syscall # stored in %eax
args for syscall in registers
       [ebx, ecx, edx, esi, edi]
 
  Execute interrupt # 128
In the interrupt vector table
 
7
 
Primer into virtual memory management
 
Virtual addresses
 
Physical addresses
 
unallocated
 
8
 
Primer into kernel and user space memory
 
Acknowledgement: http://duarts.org/gustavo/blog/category/internals
 
9
 
Primer into how context switching happens
 
Acknowledgement: http://duarts.org/gustavo/blog/category/internals
 
10
 
Flow of control during a system call invocation
 
system call
invocation
 
entry_32.S
 
Saves registers
    on stack
Save return address
 of user process
 (thread_info)
 
syscall_table.S
 
       table of
function pointers
 
system call
 execution
 
Kernel space
 
User space
 
    your
application
 
library (libc)
 
int 0x80
 
 restore
registers
 
 return value stored
 in  the stack location
corresponding to %eax
 
iret
 
Return value
Error = -1
Errorcode = errorno
 
11
 
   
   Kernel dive.
 
12
 
Important kernel files/ data structures for system calls
 
i
mplementation file for the sys call
kernel/sys.c (most of the system calls are
implemented)
You
 can implement a system call 
anywhere
 
include/asm-i386/unistd.h
Defines the *number* of a system call
Defined the total number of system calls.
 
arch/i386/kernel/syscall_table.S
Stores the system
 call table
Stores the function pointers to system call definition
 
13
 
Using sysenter/sysexit in Linux > 2.5
 
 
Sysenter/sysexit is also called “Fast system Call”
Available in Pentium II +
 
 
Sysenter is made of three registers
SYSENTER_CS_MSR  -- selecting segment of the kernel
code (figuring out which kernel code to run)
SYSENTER_EIP_MSR --- address of the kernel entry
SYSENTER_ESP_MSR --- kernel stack pointer
 
 
14
 
Simplified view of sysenter/sysexit in Linux > 2.5
 
_ _kernel_vsyscall
 
entry_32.S
 
Saves user mode
        stack
Save return address
 of user process
 (thread_info)
 
syscall_table.S
 
       table of
function pointers
 
system call
 execution
 
Kernel space
 
User space
 
    your
application
 
library (libc)
 
sysenter
 
 restore
registers
 
 return value stored
 in  the stack location
corresponding to %eax
 
sysexit
 
Return value
Error = -1
Errorcode = errorno
 
15
 
Lets write a system call in the kernel (sys_strcpy)
 
   
 
   int strcpy(char *src, char *dest, int len)
 
       
asmlinkage
 long sys_strcpy(char *src, char *dest, int len)
 
       compiler directive
 params will be read from stack
 
     can return values of
 size of at most long? Why?
 
16
 
Issues to think about when writing system calls
 
 
Moving data between the kernel and user process
Concerns
: security and protection
 
Synchronization and concurrency 
(will revisit)
Several (so called) kernel threads might be accessing
the same data structure that you want to read/write
Simple solution (disable interrupts “cli”)
Usually not a good idea
Big problem in 
preemptive
 CPU (which is almost every
CPU) and 
multi-processor 
systems
CONFIG_SMP or CONFIG_PREEMPT
 
 
 
17
 
Useful kernel API functions for bidirectional data movement
 
 
access_ok (type, addr, size)
:  type (VERIFY_READ, VERIFY_WRITE)
get_user(x, ptr) 
--- read a char or int from user-space
put_user(x, ptr) 
--- write variable from kernel to user space
copy_to_user(to, from, n) 
--- copy data from kernel to userspace
copy_from_user(to, from, n) 
– copy data to kernel from userspace
strnlen_user(src, n) 
– checks that the length of a buffer is n
strcpy_from_user(dest, src, n) 
---copies from kernel to user space
 
 
 
Acknowledgement: http://www.ibm.com/developerworks/linux/library/l-kernel-memory-access/index.html
 
18
 
Next class
 
Linux Boot process
How the first process gets started
 
Process management
Process creation and basic IPC: fork(), pipe(), dup2(),
wait()
Theory on processes
 
 
 
19
 
  
An in-class discussion
 
 (a Microsoft Interview Question)
Slide Note
Embed
Share

Explore the core concepts of operating systems, including kernel-userspace interactions, system calls, context switching, and virtual memory management. Delve into x86 assembly for system calls, hardware interrupts, and the flow of control during system call invocations. Gain insights into key components like the user space, kernel space, and the execution of syscalls in the operating system environment.


Uploaded on Aug 04, 2024 | 8 Views


Download Presentation

Please find below an Image/Link to download the presentation.

The content on the website is provided AS IS for your information and personal use only. It may not be sold, licensed, or shared on other websites without obtaining consent from the author. Download presentation by click this link. If you encounter any issues during the download, it is possible that the publisher has removed the file from their server.

E N D

Presentation Transcript


  1. CMSC421: Principles of Operating Systems Nilanjan Banerjee Assistant Professor, University of Maryland Baltimore County nilanb@umbc.edu http://www.csee.umbc.edu/~nilanb/teaching/421/ Principles of Operating Systems 1

  2. Announcements Project 0 and Homework 1 out Discussion grades will not be on Blackboard Readings from Silberchatz Optional but important 2

  3. Discussion 1 Sign bit exponent Mantissa 1000 *(int *)&a 1000 1000 Integer/2s compliment 3

  4. Kernel-userspace interaction software interrupts system calls Hardware interrupts timers Application kernel exceptions Timers/return from sys call A closer look at system calls 4

  5. Lets take an example main: OS _start libc linker assembler .s file .o file Exec. .c file libraries 5

  6. X86 assembly for system calls (older mechanism) mov $1, %eax mov $25, %ebx int $0x80 Execute interrupt # 128 In the interrupt vector table Software interrupt Jump to an address in the kernel where the syscall table is stored And execute syscall # stored in %eax args for syscall in registers [ebx, ecx, edx, esi, edi] 6

  7. Primer into virtual memory management Virtual addresses unallocated Physical addresses 7

  8. Primer into kernel and user space memory Acknowledgement: http://duarts.org/gustavo/blog/category/internals 8

  9. Primer into how context switching happens Acknowledgement: http://duarts.org/gustavo/blog/category/internals 9

  10. Flow of control during a system call invocation library (libc) Return value Error = -1 Errorcode = errorno your system call invocation application User space int 0x80 Kernel space syscall_table.S iret entry_32.S Saves registers on stack Save return address of user process (thread_info) restore registers system call execution return value stored in the stack location corresponding to %eax table of function pointers 10

  11. Kernel dive. 11

  12. Important kernel files/ data structures for system calls implementation file for the sys call kernel/sys.c (most of the system calls are implemented) You can implement a system call anywhere include/asm-i386/unistd.h Defines the *number* of a system call Defined the total number of system calls. arch/i386/kernel/syscall_table.S Stores the system call table Stores the function pointers to system call definition 12

  13. Using sysenter/sysexit in Linux > 2.5 Sysenter/sysexit is also called Fast system Call Available in Pentium II + Sysenter is made of three registers SYSENTER_CS_MSR -- selecting segment of the kernel code (figuring out which kernel code to run) SYSENTER_EIP_MSR --- address of the kernel entry SYSENTER_ESP_MSR --- kernel stack pointer 13

  14. Simplified view of sysenter/sysexit in Linux > 2.5 library (libc) Return value Error = -1 Errorcode = errorno your _ _kernel_vsyscall application User space sysenter Kernel space syscall_table.S sysexit entry_32.S Saves user mode stack Save return address of user process (thread_info) restore registers system call execution return value stored in the stack location corresponding to %eax table of function pointers 14

  15. Lets write a system call in the kernel (sys_strcpy) int strcpy(char *src, char *dest, int len) can return values of size of at most long? Why? asmlinkage long sys_strcpy(char *src, char *dest, int len) compiler directive params will be read from stack 15

  16. Issues to think about when writing system calls Moving data between the kernel and user process Concerns: security and protection Synchronization and concurrency (will revisit) Several (so called) kernel threads might be accessing the same data structure that you want to read/write Simple solution (disable interrupts cli ) Usually not a good idea Big problem in preemptive CPU (which is almost every CPU) and multi-processor systems CONFIG_SMP or CONFIG_PREEMPT 16

  17. Useful kernel API functions for bidirectional data movement access_ok (type, addr, size): type (VERIFY_READ, VERIFY_WRITE) get_user(x, ptr) --- read a char or int from user-space put_user(x, ptr) --- write variable from kernel to user space copy_to_user(to, from, n) --- copy data from kernel to userspace copy_from_user(to, from, n) copy data to kernel from userspace strnlen_user(src, n) checks that the length of a buffer is n strcpy_from_user(dest, src, n) ---copies from kernel to user space Acknowledgement: http://www.ibm.com/developerworks/linux/library/l-kernel-memory-access/index.html 17

  18. Next class Linux Boot process How the first process gets started Process management Process creation and basic IPC: fork(), pipe(), dup2(), wait() Theory on processes 18

  19. (a Microsoft Interview Question) An in-class discussion 19

More Related Content

giItT1WQy@!-/#giItT1WQy@!-/#giItT1WQy@!-/#giItT1WQy@!-/#giItT1WQy@!-/#