Page-Faults in Linux How can we study the handling of page-fault exceptions?

Slides:



Advertisements
Similar presentations
Processor Privilege-Levels
Advertisements

Types of Code Segments Conforming Code Segment
1/1/ / faculty of Electrical Engineering eindhoven university of technology Memory Management and Protection Part 3:Virtual memory, mode switching,
Unit 4 Chapter-1 Multitasking. The Task State Segment.
Intel MP.
Microprocessors system architectures – IA32 real and virtual-8086 mode Jakub Yaghob.
OS Memory Addressing.
Chapter 6 Limited Direct Execution
IA32 Paging Scheme Introduction to the Pentium’s support for “virtual” memory.
CS591 (Spring 2001) The Linux Kernel: Signals & Interrupts.
Task-Switching How the x86 processor assists with context-switching among multiple program-threads.
Interrupts in Protected-Mode Writing a protected-mode interrupt-service routine for the timer-tick interrupt.
Operating Systems: Segments 1 Segmentation Hardware Support single user program system: – wish somehow to relocate address 0 to after operating system.
Processor Exceptions A survey of the x86 exceptions and mechanism for handling faults, traps, and aborts.
IA-32 Processor Architecture
Context Switch Animation Another one by Anastasia.
Exceptions and Interrupts How does Linux handle service- requests from the cpu and from the peripheral devices?
Interrupts in Protected-Mode Writing a protected-mode interrupt-service routine for the timer-tick interrupt.
Venturing into protected-mode A first look at the CPU registers and instructions which provide the essential supporting infrastructure.
X86 segmentation, page tables, and interrupts 3/17/08 Frans Kaashoek MIT
Task-Switching How the x86 processor assists with context-switching among multiple program-threads.
Linux Operating System
Linux Operating System
Venturing into protected-mode
Microprocessor Systems Design I Instructor: Dr. Michael Geiger Fall 2012 Lecture 15: Protected mode intro.
UNIT 2 Memory Management Unit and Segment Description and Paging
System Calls 1.
80386DX.
Windows Kernel Internals Traps, Interrupts, Exceptions
Microprocessor system architectures – IA32 segmentation Jakub Yaghob.
Multitasking Mr. Mahendra B. Salunke Asst. Prof. Dept. of Computer Engg., STES SITS, Narhe, Pune-41 STES Sinhgad Institute of Tech. & Science Dept. of.
CSC 660: Advanced Operating SystemsSlide #1 CSC 660: Advanced OS Memory Addressing / Kernel Modules.
80386DX.
1 i386 Memory Management Professor Ching-Chi Hsu 1998 年 4 月.
Virtual 8086 Mode  The supports execution of one or more 8086, 8088, 80186, or programs in an protected-mode environment.  An 8086.
EFLAG Register of The The only new flag bit is the AC alignment check, used to indicate that the microprocessor has accessed a word at an odd.
Microprocessor system architectures – IA32 tasks Jakub Yaghob.
80386DX. Programming Model The basic programming model consists of the following aspects: – Registers – Instruction Set – Addressing Modes – Data Types.
Segment Descriptor Segments are areas of memory defined by a programmer and can be a code, data or stack segment. In segments need not be all the.
Functions/Methods in Assembly
6. HAL and IDT ENGI 3655 Lab Sessions. Richard Khoury2 Textbook Readings  Interrupts ◦ Section  Hardware Abstraction Layer ◦ Section
D P L s G D X U P Segment Descriptor A T Y P E
1 Microprocessors CSE Protected Mode Memory Addressing Remember using real mode addressing we were previously able to address 1M Byte of memory.
Page Replacement Implementation Issues Text: –Tanenbaum ch. 4.7.
Chapter 2 The Microprocessor Architecture Microprocessors prepared by Dr. Mohamed A. Shohla.
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.
What You Need to Know for Project Three Steve Muckle Wednesday, February 19 th Spring 2003.
Lecture 5 Rootkits Hoglund/Butler (Chapters 1-3).
Information Security - 2. Task Switching Every process has an associated Task State Segment, whose starting point is stored in the Task register. A task.
Memory Management Unit and Segment Description and Paging
Information Security - 2. Descriptor Tables Descriptors are stored in three tables: – Global descriptor table (GDT) Maintains a list of most segments.
OS Memory Addressing. Architecture CPU – Processing units – Caches – Interrupt controllers – MMU Memory Interconnect North bridge South bridge PCI, etc.
Microprocessors CSE- 341 Dr. Jia Uddin Assistant Professor, CSE, BRAC University Dr. Jia Uddin, CSE, BRAC University.
The Microprocessor & Its Architecture A Course in Microprocessor Electrical Engineering Department Universitas 17 Agustus 1945 Jakarta.
Homework / Exam Return and Review Exam #1 Reading Machine Projects
Assembly language.
Descriptor Table & Register
16.317: Microprocessor System Design I
Operating Systems Engineering
Microprocessor Systems Design I
Anton Burtsev February, 2017
Anton Burtsev February, 2017
143A: Principles of Operating Systems Lecture 5: Address translation
x86 segmentation, page tables, and interrupts
System Segment Descriptor
Page Replacement Implementation Issues
Page Replacement Implementation Issues
Information Security - 2
Low-Level Thread Dispatching on the x86
CS444/544 Operating Systems II Virtual Memory
Presentation transcript:

Page-Faults in Linux How can we study the handling of page-fault exceptions?

Why page-faults happen Trying to access a virtual memory-address Instruction-operand / instruction-address Read-data/write-data, or fetch-instruction Maybe page is ‘not present’ Maybe page is ‘not readable’ Maybe page is ‘not writable’ Maybe page is ‘not visible’

Page-fault examples movl%eax, (%ebx); writable? movl(%ebx), %eax; readable? jmpahead; present? Everything depends on the entries in the current page-directory and page-tables, and on the cpu’s Current Privilege Level

Current Privilege Level (CPL) segment-selector RPL TITI TI = Table-IndicatorRPL=Requested Privilege Level Layout of segment-register contents (16 bits) CPL is determined by the value of RPL field in CS and SS

What does the CPU do? Whenever the cpu detects a page-fault, its action depends on Current Privilege Level If CPL == 0 (executing in kernel mode): 1) push EFLAGS register 2) push CS register 3) push EIP register 4) push error-code 5) jump to page-fault service-routine

Alternative action in user-mode If CPL == 3 (executing in user mode) the CPU will switch to its kernel-mode stack: 0) push SS and ESP 1) push EFLAGS 2) push CS 3) push EIP 4) push error-code 5) jump to the page-fault service-routine

How CPU finds new stack Special CPU segment-register: TR TR is the ‘Task Register’ TR holds ‘selector’ for a GDT descriptor Descriptor is for a ‘Task State Segment’ So TR points indirectly to current TSS TSS stores address of kernel-mode stack

Stack Switching mechanism GDTR TSS descriptor TR ESP0 SS0 TASK STATE SEGMENT GLOBAL DESCRIPTOR TABLE IDTR INTERRUPT DESCRIPTOR TABLE Gate descriptor CSEIP SSESP kernel stack kernel code user code user stack user-space kernel-space

Let’s ‘intercept’ page-faults Use our systems programming knowledge We build a ‘new’ Interrupt Descriptor Table With our own ‘customized’ interrupt-gates Use a ‘new’ gate for page-fault exceptions Other existing gates we can simply copy Why not just modify the existing IDT? It’s ‘write-protected’ in some Linux kernels But we can still ‘read’ it (i.e., for copying)

Very delicate to implement Will need to use some assembly language Using C language doesn’t give full control C Compiler designers didn’t plan for this! (except they did allow for using assembly) Assembly requires us to be very precise So try keeping assembly to a minimum We can use a mixture of assembly and C

Allocate a mapped page Device interrupts are ‘asynchronous’ CPU requires instant access to the IDT We must insure CPU can find new IDT Cannot risk putting it in ‘high memory’ We can use ‘get_free_page()’ function With flags: GFP_KERNEL and GFP_DMA (This insures page will be always mapped) No memory available? Cannot continue.

Must find address of current IDT We’ll need it for copying the existing gates We’ll need it for restoring old IDT upon exit We can use the ‘sidt’ instruction to find it But ‘sidt’ needs a 48-bit memory-operand No such type is directly supported in C We could use a 64-bit type (i.e., long long) Better to use array of three 16-bit values

Getting hold of current IDT We need to declare a global variable Because ‘init_module()’ needs it And also ‘cleanup_module()’ needs it Use ‘static’ to make it private Use ‘short’ to get 16-bit array-entries Use ‘unsigned’ to avoid sign-extensions static unsigned short oldidtr[ 3 ];

Activating a ‘new’ IDT When we’re ready, we can use ‘sidt’ Instruction will change the IDTR register Instruction needs 48-bit memory operand So again we will declare a suitable array static unsigned short newidtr[ 3 ];

Initializations We need to initialize our ‘idtr’ array We need to initialize new Descriptor Table Use ‘memcpy()’ for copying within kernel Page-Fault’s gate-descriptor must be built Must conform to CPU’s expected layout Need to use a local 64-bit variable unsigned long longgate_desc;

Format for a Gate Descriptor segment-selectoroffset[ 15…0 ] offset[ 31…16 ] gate type 063 Quadword (64-bits) The address of the fault-handler is ‘split’ into a hiword and a loword

Declaring our fault-handler Tell the C compiler our handler’s name: asmlinkage void isr0x0E( void ); Its type and value are set by assembler: asm(“.text“); “); asm(“isr0x0E:“);

Save/Restore cpu registers Upon entering: asm(“pushal“); asm(“pushl%ds“); asm(“pushl%es“); Upon leaving: asm(“popl%es“); asm(“popl%ds“); asm(“popal“); asm(“jmp*old_isr“);

Handler must access kernel data Registers CS and SS get set up by the CPU But its our job to set up DS and ES registers Linux uses same segments for data and stack asm(“ mov%ss, %eax“); asm(“ mov%eax, %ds“); asm(“ mov%eax, %es“); (Current kernel version doesn’t use FS or GS)

Transfer to a C function Handler will need some info from the stack The ‘error-code’ will be needed for sure So C function will need an ‘argument’ So here’s our C function prototype: static void handler( unsigned long *tos );