Lecture 7 Interrupt ,Trap and System Call

Slides:



Advertisements
Similar presentations
计算机系 信息处理实验室 Lecture 3 System Mechanisms (1)
Advertisements

More on Processes Chapter 3. Process image _the physical representation of a process in the OS _an address space consisting of code, data and stack segments.
CS 4284 Systems Capstone Godmar Back Processes and Threads.
1 Exceptions, Interrupts & Traps Operating System Hebrew University Spring 2007.
Tutorial 3 - Linux Interrupt Handling -
Exceptional Control Flow Processes Today. Control Flow Processors do only one thing: From startup to shutdown, a CPU simply reads and executes (interprets)
Using VMX within Linux We explore the feasibility of executing ROM-BIOS code within the Linux x86_64 kernel.
Interrupts Chapter 8 – pp Chapter 10 – pp Appendix A – pp 537 &
Unit 4 Chapter-1 Multitasking. The Task State Segment.
CS 153 Design of Operating Systems Spring 2015 Lecture 3: Intro and Architectural Support for Operating Systems.
Features of Intel Processor Architectures that Lend to Operating System Design Jim Snyder.
Architectural Support for OS March 29, 2000 Instructor: Gary Kimura Slides courtesy of Hank Levy.
OS Spring’03 Introduction Operating Systems Spring 2003.
Advanced OS Chapter 3p2 Sections 3.4 / 3.5. Interrupts These enable software to respond to signals from hardware. The set of instructions to be executed.
Hardware Support for Operating Systems Sunny Gleason Vivek Uppal COM S 414
Introduction to Interrupts
Computer System Laboratory
I/O Tanenbaum, ch. 5 p. 329 – 427 Silberschatz, ch. 13 p
What are Exception and Interrupts? MIPS terminology Exception: any unexpected change in the internal control flow – Invoking an operating system service.
Intel IA32 OS Support -Refresh
Interrupts. What Are Interrupts? Interrupts alter a program’s flow of control  Behavior is similar to a procedure call »Some significant differences.
Recall: Three I/O Methods Synchronous: Wait for I/O operation to complete. Asynchronous: Post I/O request and switch to other work. DMA (Direct Memory.
Operating Systems ECE344 Ashvin Goel ECE University of Toronto OS-Related Hardware.
Lecture 3 Process Concepts. What is a Process? A process is the dynamic execution context of an executing program. Several processes may run concurrently,
1 CSE 451 Section 2: Interrupts, Syscalls, Virtual Machines, and Project 1.
CSC 2405 Computer Systems II Exceptions Mini-Lecture Traps & Interrupts.
CSNB374: Microprocessor Systems Chapter 5: Procedures and Interrupts.
Lab 13 Department of Computer Science and Information Engineering National Taiwan University Lab13 – Interrupt + Timer 2014/12/23 1 /16.
Sogang University Advanced Operating Systems (Enhanced Device Driver Operations) Advanced Operating Systems (Enhanced Device Driver Operations) Sang Gue.
System Calls. The Linux we use is: Linux-Mandrake 7.0. In this project, you are going to change some kernel files and recompile the kernel. After you.
Interrupt driven I/O. MIPS RISC Exception Mechanism The processor operates in The processor operates in user mode user mode kernel mode kernel mode Access.
Exceptional Control Flow Topics Exceptions except1.ppt CS 105 “Tour of the Black Holes of Computing”
6-1 Infineon 167 Interrupts The C167CS provides 56 separate interrupt sources that may be assigned to 16 priority levels. The C167CS uses a vectored interrupt.
Dec Hex Bin 14 E ORG ; FOURTEEN Interrupts In x86 PC.
1 CE6105 Linux 作業系統 Linux Operating System 許 富 皓.
6. HAL and IDT ENGI 3655 Lab Sessions. Richard Khoury2 Textbook Readings  Interrupts ◦ Section  Hardware Abstraction Layer ◦ Section
10. Epilogue ENGI 3655 Lab Sessions.  We took control of the computer as early as possible, right after the end of the BIOS  Our multi-stage bootloader.
Lecture 5 Rootkits Hoglund/Butler (Chapters 1-3).
BIOS and DOS Interrupts Basic Input /Outpu System Disk Operating System.
Interrupts and Exception Handling. Execution We are quite aware of the Fetch, Execute process of the control unit of the CPU –Fetch and instruction as.
Introduction to Exceptions 1 Introduction to Exceptions ARM Advanced RISC Machines.
Interrupts and Exceptions 國立中正大學 資訊工程研究所 羅習五 老師. Interrupt & Exceptions Interrupts – maskable & nonmaskable interrupt Exceptions – Processor-detected.
Of Privilege, Traps, Interrupts & Exceptions Prof. Sirer CS 316 Cornell University.
Virtualizing the CPU: Processes 1. How to provide the illusion of many CPUs? CPU virtualizing – The OS can promote the illusion that many virtual CPUs.
Exceptional Control Flow
Homework / Exam Return and Review Exam #1 Reading Machine Projects
Linux Kernel Development - Robert Love
MICROPROCESSOR BASED SYSTEM DESIGN
Microprocessor and Assembly Language
Operating Systems Engineering
Exceptional Control Flow
Microprocessor Systems Design I
Protection and OS Structure
Exceptional Control Flow
Anton Burtsev February, 2017
Protection of System Resources
Exceptional Control Flow
Anton Burtsev February, 2017
Interrupts In 8085 and 8086.
Overview of today’s lecture
CS 3305 System Calls Lecture 7.
Exceptional Control Flow
Exceptional Control Flow: System Calls, Page Faults etc.
Discussions on HW2 Objectives
CSE 153 Design of Operating Systems Winter 18
Exceptions and Processes
CSE 451: Operating Systems Autumn 2003 Lecture 2 Architectural Support for Operating Systems Hank Levy 596 Allen Center 1.
Discussions on HW2 Objectives
CSE 451: Operating Systems Winter 2003 Lecture 2 Architectural Support for Operating Systems Hank Levy 412 Sieg Hall 1.
Interrupts and System Calls
Presentation transcript:

Lecture 7 Interrupt ,Trap and System Call Oct. 27, 2015 Kyu Ho Park Lecture 7 Interrupt ,Trap and System Call

interrupt There are 2 kinds of interrupts; 1. Synchronous interrupts often called exceptions. [Generated by the CPU] 2. Asynchronous interrupts often called interrupts. [Generated by other H/W devices]

interrupt -External interrupt: issued by peripheral devices. -Trap: It is also called exception handling such as ‘divide by zero’, ‘segmentation faults’, ‘page faults’ and ‘system calls’. (trap is called also ‘software interrupt) -Interrupts and Traps are all handled in the same way at Linux.

Kernel H/W Interrupt handling (x86 CPU) sys_call_table H/W IDT(IVT) sys_restart_syscall sys_exit sys_write idt_table devide_error debug nmi int3 system_call . CPU 1 . 2 . action 3 PIC N . action irq_desc timer_interrupt floppy_intrrupt floppy_interrupt 32 . . do_IRQ . 128 action . action . . 223 255 0~31 entry of IDT are for trap handler

Interrupt numbers 0~19 : Intel CPU uses. 20~31: Reserved for Intel CPU. 32~255: Allocated to OS.

Interrupt Vector Table Process Interrupt ISR Disable Interrupt Save Processor states Interrupt handling Return from Interrupt Restore Processor States Enable Interrupt

irq handling procedure common_interrupt: SAVE_ALL call do_IRQ jump ret_from_intr SAVE_ALL: pushl %es; pushl %ds; pushl %eax; pushl %ebp; pushl %edi; pushl %esi; pushl %edx; pushl %ecx; movl $(_USER_DS), %edx; movl %edx, %ds; movl %edx, %es;

interrupt Interrupt: -external interrupt [ret_from_intr( )] -trap -fault: page fault [ret_from_exception( )] -trap: system call [ret_from_sys_call( )] -abort: divide by zero

Interrupt handling Exception ‘divide_by_zero(DVZ)’  IDT(Interrupt Description Table) DVZ handling routine Interrupt  IDT  irq_desc  irq service routine System call  IDT  sys_call_table sys_call service routine

Installing interrupt handler Declared in <linux/interrupt.h> int request_irq(unsigned int irq, void (*handler)(int, void *, struct pt_regs *), unsigned long flags, const char *dev_name,void *dev_id); , where irq: IRQ number, handler:interrupt handling function, flags: SA_INTERRUPT : Interrupt disables on the current processor, SA_SHIRQ: share the same IRQ numbers which means the interrupt can be shared between devices, SA_SAMPLE_RANDOM: if your device generates interrupts at truly random times, you should set this flags. dev_name: the string is passed to request_irq is used in /proc/interrupts to show the owner of the interrupt. dev_id: pointer used for shared interrupt lines. If the interrupt is not shared , set it to NULL. return value : 0: successful or a negative error code. void free_irq(unsigned int irq, void *dev_id);

Interrupt handler function irqreturn_t int_interrupt(int irq, void *dev_id, struct pt_reg *regs) { … return IRQ_HANDLED; } - regs: all register values of CPU are stored at regs.

Kernel H/W Interrupt handling (x86 CPU) sys_call_table H/W IDT(IVT) sys_restart_syscall sys_exit sys_write idt_table devide_error debug nmi int3 system_call . CPU 1 . 2 . action 3 PIC N . action irq_desc timer_interrupt floppy_intrrupt floppy_interrupt 32 . . do_IRQ . 128 action . action . . 223 255 0~31 entry of IDT are for trap handler

request_irq( irq, int_handler, request_irq( irq, int_handler, ..) function registers the IRQ number and its handler to irq_descriptor table. free_irq( ) removes the irq_handler from the ire_descriptor table. When a interrupt occurs, do_IRQ( ) function is called and it calls the interrupt handler registered in irq_descriptor table.

How to check the status of interrupt #cat /proc/interrupts

External interrupt

Exception Vector Table

Interrupt handling flow

intr handling procedure ARM CPU Interrupt Vector Table(IVT) _start: b reset b undefined _instruction b software_interrupt b prefetch_abort b data_abort b not_used b IRQ b FIQ

Registration and freeing irq_handler int request_irq(unsigned int irq, irq_handler_t handler, unsigned long flags,const char *name, void *dev); void free_irq(unsigned int, void *); EX: if (request_irq(IRQ_EINT1,&button_isr,SA_INTERRUPT, "INT_BUTTON_TEST", NULL)) printk(KERN_ERR "INT_BUTTON_TEST: IRQ1 Registration filed!\n"); free_irq(IRQ_EINT1, NULL);

isr_test_button.c(1)

isr_test_button.c(3)

System calls System calls are function invocations made from user space into kernel in order to request some service or resource from OS. CPU provides 4 privilege levels(Linux uses only level 0 and 3). return Level 0 1 2 3 User space Kernel space System call

system_call handling (x86) user task main( ) {… fork( ); } libc.a - fork( ) { movl 2,%eax int $0x80 Kernel ENTRY(system_call) /*arch/x86/kernel/entry_32.S*/ SAVE_ALL - call *SYMBOL_NAME(sys_call_table)(,%eax,4) ret_from_sys_call(schedule,signal,……) IDT 0x0 divide_error( ) nmi( ) 0x80 system_call( ) sys_call_table 1 sys_exit( ) sys_fork( ) /*kernel/fork.c*/ 2 sys_fork( ) 3 sys_read( ) sys_write( )

System Call Interface for user applications To access kernel or hardware devices We will implement it in the Linux source code Which you’ve already used in Project 2 Only for 64-bit VM USER Application System Call Kernel System Call Interface Access Hardware Hardware Device

How to add a new system call 1. Add a entry at “arch/x86/syscalls/syscall_64.tbl”. 2. Add a entry at the last line of the file “include/linux/syscalls.h”. 3. Create a file and implement the system call handler at “kernel/mysyscall.c”. 4. Add mysyscall.o to “obj-y” at “kernel/Makefile”. 5. Compile , install and reboot the kernel.

How to Add System Call (1) arch/x86/syscalls/syscall_64.tbl Add entry Remember the number (it should be less then 512, and should not be same as others)

How to Add System Call (2) include/linux/syscalls.h Add entry at the last part of the file Use function name as previous step

How to Add System Call (3) kernel/mysyscall.c Create file and implement system call handler Use function name as previous step

How to Add System Call (4) kernel/Makefile Add entry to “obj-y” Use name from the created file name

How to Add System Call (5) Compile and install # make -j4 bzImage # make install Reboot

How to Use New System Call (1) Write test program in the user level Use same number as before

How to Use New System Call (2) Compile and run

Make Your Own System Call Make a system call User gives two integer values System call handler Prints information including your student ID Returns results of addition of two integer values given by user User prints returned value Example result

System call implementation for ARM CPU 1. Allocation of system call number /home/beagleboard/linuxkernel/stable-kernel/linux-stable/include/asm-generic/unistd.h #define __NR_newsyscall XXX 2. Register the new system call to sys_call_table /home/beagleboard/linuxkernel/stable-kernel/linux-stable/arch/arm/kernel/calls.S CALL(sys_newsyscall) 3. /home/beagleboard/linuxkernel/stable-kernel/linux-stable/kernel#vi newsyscall.c #include <linux/kernel.h> asmlinkage int sys_newsyscall( ) { printk(“newsyscall: Hello!\n”); return 21; } 4. Register the function on /linux-stable/include/linux/syscall.h asmlinkage long sys_newsyscall( ); 5. vi kernel/Makefile obj-y =………………. newsyscall.o 6. Make bzImage