Lesson 3 Laboratory work in TDDI04 Assignment 3 » Lesson purposes n n n n »Viacheslav Izosimov »2009-02-23 »viaiz@ida.liu.se Threads Pintos organization User programs » Smallest execution unit » Own stack » User-thread run in a process address User memory User stack Threads Scheduler Short repetition of Processes and Threads Virtual memory layout in PINTOS Protection of system calls Exercises about virtual memory and protection/security space (user-mode) Kernel stack Synchronization User-exception handler primitives Memory management Interrupt handler File system Timer Threads » Kernel-thread run OS-code in physical address space (kernel-mode) » Share memory with all threads in same address space Processes A system contains several threads running in parallel Thread is a “basic unit of CPU utilization” §Thread ID §A program counter §A register set §A stack Thread 1 Thread 2 » Program in execution » Own stack » Define own independent address space » Powered by kernel thread(s) » Can contain several user threads » Run in user mode Thread execution sequence 1 Process and thread relations System call Process memory, Logic address space User thread User thread Process User thread User Mode Process resources controlled by kernel » A ”system call” is the request for this service, and handling thereof Process Process » Process run in user mode » Process need access to shared System call interface Kernel thread Kernel thread Kernel thread Kernel thread Kernel Mode Physical memory address space Create example » bool create (const char *file, unsigned initial_size) » Example: create(“file.txt”, 1000); How to get them? Answer: f->esp Hint: … note that, in order to get How to return a value? a string, you will need get a void pointer from esp and then get a char pointer to which Answer: f->eax points that void pointer. The char pointer will point to the first element of the string … Create example » void SysCall_Create(struct intr_frame *f) { » char *file_name; » int file_size; » printf("create system call\n"); » file_name = (char *)*(int *)(f->esp + 4); » file_size = *(int *)(f->esp + 8); » if (filesys_create(file_name, file_size)) f->eax = true; » else f->eax = false; » } » … » case SYS_CREATE: » SysCall_Create(f); » break; » … Problem 1: If the pointer above PHYS_BASE, It points to Kernel memory! UNSAFE! All the pointers on the variables, which you get from the user program, must be validated! 2 Memory issues in Pintos Useful files » Have a look at: Kernel VM Physical Memory Kernel process PHYS_BASE userprog/pagedir.c – implements functions to deal with page directories n threads/vaddr.h – contains functions and macros for working with virtual addresses n Check if the pointer is below User process Page directory Check if the pointer is in the page directory If no entry? a) Kill user b) Handle page fault STRUCT THREAD Shutdown security (safety?) » static void syscall_handler (struct intr_frame *f) { » int syscall_name; You have to implement » validate_user_ptr(f->esp); “validate_user_ptr(void *)” function to check if » syscall_name = *(int *)(f->esp); the pointer is » switch(syscall_name) { in the user space and » case SYS_HALT: in the page directory! » printf("halt system call\n"); » power_off(); » break; » default: » printf("unknown syscall %d\n", syscall_name); » } » } Create security Other system calls » void SysCall_Create(struct intr_frame *f) { » char *file_name; A string can span across » int file_size; several pages in memory! » printf("create system call\n"); » validate_user_ptr(f->esp + 4) » » » » » » » } » Don’t forget validation of system call parameters! validate_user_ptr_str((void *)*(int *)(f->esp + 4)) » Validate buffers for read and write file_name = (char *)*(int *)(f->esp + 4); validate_user_ptr(f->esp + 8) file_size = *(int *)(f->esp + 8); if (filesys_create(file_name, file_size)) f->eax = true; else f->eax = false; system calls! » File IDs of one process must not be accessible by other processes All pointers of the string have to be valid. You should provide an efficient solution to validate as less string pointers as possible! 3 Exercises! Exit(SUCCESS); » Solve problems step by step » Discuss each sub-problem » Discuss different solutions » Answer questions 4