Linux 운영체제 및 응용 Lecture Note 2 Memory Addressing 오상규 정보통신대학원.

Slides:



Advertisements
Similar presentations
Memory.
Advertisements

Memory Management Unit
16.317: Microprocessor System Design I
4/14/2017 Discussed Earlier segmentation - the process address space is divided into logical pieces called segments. The following are the example of types.
Intel MP.
OS Memory Addressing.
UQC152H3 Advanced OS Memory Management under Linux.
IA-32 Processor Architecture
Chapter 8.3: Memory Management
Memory Management (II)
CE6105 Linux 作業系統 Linux Operating System 許 富 皓. Chapter 2 Memory Addressing.
Chapter 3.2 : Virtual Memory
CS2422 Assembly Language & System Programming September 22, 2005.
Microprocessor Systems Design I Instructor: Dr. Michael Geiger Fall 2012 Lecture 15: Protected mode intro.
Virtual Memory  Early computers had a small and fixed amount to memory. All programs had to be able to fit in this memory. Overlays were used when the.
UNIT 2 Memory Management Unit and Segment Description and Paging
Memory Addressing in Linux  Logical Address machine language instruction location  Linear address (virtual address) a single 32 but unsigned integer.
8.4 paging Paging is a memory-management scheme that permits the physical address space of a process to be non-contiguous. The basic method for implementation.
1 Chapter 3.2 : Virtual Memory What is virtual memory? What is virtual memory? Virtual memory management schemes Virtual memory management schemes Paging.
Chapter 8 – Main Memory (Pgs ). Overview  Everything to do with memory is complicated by the fact that more than 1 program can be in memory.
CS 6560 Operating System Design Lecture 8: Memory Management.
CSC 660: Advanced Operating SystemsSlide #1 CSC 660: Advanced OS Memory Addressing / Kernel Modules.
1 Linux Operating System 許 富 皓. 2 Memory Addressing.
Memory Addressing in Linux (Chap. 2, Understanding the Linux Kernel) J. H. Wang Oct. 20, 2008.
CE Operating Systems Lecture 14 Memory management.
80386DX.
Memory Addressing in Linux (Chap. 2 in Understanding the Linux Kernel) J. H. Wang Oct. 15, 2009.
1 i386 Memory Management Professor Ching-Chi Hsu 1998 年 4 月.
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.
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.
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.
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.
Basic Paging (1) logical address space of a process can be made noncontiguous; process is allocated physical memory whenever the latter is available. Divide.
32- bit Microprocessor-Intel 80386
Memory Hierarchy Ideal memory is fast, large, and inexpensive
Memory Management.
Non Contiguous Memory Allocation
CSE 120 Principles of Operating
Lecture 12 Virtual Memory.
CSNB334 Advanced Operating Systems 5. Memory Management
Outline Paging Swapping and demand paging Virtual memory.
COMBINED PAGING AND SEGMENTATION
Address Translation Mechanism of 80386
x86 segmentation, page tables, and interrupts
Page Replacement Implementation Issues
CSE 153 Design of Operating Systems Winter 2018
Chapter 8: Main Memory.
Operating System Concepts
Operating Modes UQ: State and explain the operating modes of X86 family of processors. Show the mode transition diagram highlighting important features.(10.
Background Program must be brought into memory and placed within a process for it to be run. Input queue – collection of processes on the disk that are.
Computer Architecture
Main Memory Background Swapping Contiguous Allocation Paging
MICROPROCESSOR MEMORY ORGANIZATION
Microprocessor and Assembly Language Addressing Models
Page Replacement Implementation Issues
Introduction to the Intel x86’s support for “virtual” memory
Contents Memory types & memory hierarchy Virtual memory (VM)
Chapter 8: Memory Management strategies
CSE451 Virtual Memory Paging Autumn 2002
CS703 - Advanced Operating Systems
Linux 운영체제 및 응용 Lecture Note 2 Memory Addressing 오상규 정보통신대학원.
CSE 153 Design of Operating Systems Winter 2019
Assembly Language for Intel-Based Computers, 5th Edition
COMP755 Advanced Operating Systems
Chapter 8: Main Memory CSS503 Systems Programming
CS444/544 Operating Systems II Virtual Memory
Virtual Memory 1 1.
Presentation transcript:

Linux 운영체제 및 응용 Lecture Note 2 Memory Addressing 오상규 정보통신대학원

What is Memory Addressing? Definition of Memory Addressing An activity or mechanism for the CPU to find a real location to store or retrieve data to/from on a physical memory space Address translation from logical address to physical address Derived from adopting virtual memory mechanism; no address translation required with physical memory system One of three memory related management mechanisms in operating systems: others are memory management and process address space Addressing = mapping = indirection; a core technology enabling general purpose computing Every single virtual technology is enabled by an “indirection” technology. Physical Memory Addressing Virtual Memory Addressing FFFFFFFF FFFF FFFF x x … move (ax, x) Mapping or Addressing CPU Instruction y 0000 0000 Physical Memory Physical Memory 00000000 Virtual Memory

Hardware Segmentation Unit Memory Addresses – 1/2 Memory address resolution Memory address resolution is required ever since virtual memory concept was first introduced. Operating systems do not have to keep track of physical address by itself fortunately since today’s microprocessor technology provides several hardware circuits to make memory management both more efficient and more robust so that programming errors cannot cause improper accesses to memory outside the program. Memory addresses In general, memory address implies a way to access the contents of a memory cell – in reality, there are a number of different types of memory address; three memory address types with 80x86 microprocessor Logical address Memory addresses embedded in the machine language instructions consisted of a segment and an offset (or displacement) based on the well-known 80x86 segmented architecture Linear address (or virtual address) A single 32-bit (addressable up to 4GB or 4,294,967,296 memory cells) unsigned integer usually represented in hexadecimal notation (0x00000000 ~ 0xFFFFFFFF) Physical address A single 32-bit or 36-bit unsigned integer locating physical memory cells in memory chips which corresponds to the electrical signals sent along the address pins of the microprocessor to the memory bus Hardware Segmentation Unit Hardware Paging Unit Logical Address Linear Address Physical Address

Memory Addresses – 2/2 Memory management unit (MMU) MMU consists of the segmentation unit and the paging unit both of which are hardware circuits. The segmentation unit translates a logical address into a linear address which is in turn translated into a physical address by the paging unit. Memory arbiter In UMA architecture, the same memory is shared by more than one CPU or DMA controllers implying that RAM chips may be accessed concurrently by multiple CPUs or DMA controllers. Read and write operations on a RAM chip must be performed serially; one CPU or DMA controller at a time – a hardware circuit called memory arbiter is inserted between the bus and every RAM chip in order to grant an exclusive access to a specific CPU or DMA controller at a time. The more CPUs are there, the more complex the structure of the arbiter is because it requires more input ports. From the programming point of view, the existence of the arbiter is hidden because it is managed by hardware circuits. RAM0 RAM1 RAM2 RAM3 CPU0 CPU1 CPU2 CPU3 Arbitor0 Arbitor1 Arbitor2 Arbitor3

Memory Addressing Scenario Snapshot (Animation) … foo( ); main( ) x = goo(y); return( ); foo( ) return(z); goo(y) Virtual Memory Space 0xFFFFFFFF … exit jmp 0x00050000 (pop z) exit (push z) pop y jmp 0x00060000 PC:0x00000011 PC:0x00000012 PC:0x00020000 PC:0x000200F2 PC:0x000200F3 PC:0x00010000 PC:0x00010001 0x00010000 0x000A0000 0x00040000 0x00040011 main( ) code 0x00050000 goo( ) code 0x0005002F 0x00060000 foo( ) code 0x000600F2 0x000600F3 0x00060120 PC:0x00020120 PC:0x0001002F PC:0x00000000 SP:0x00000000 + SP:0x00000002 SP:0x00000001 SP:0x00000003 + CS Segment Descriptor DS Segment Descriptor SS Segment Descriptor BASE CS:0x00040000 DS:0x000A0000 SS:0x00010000 … SS Segment Descriptor DS Segment Descriptor CS Segment Descriptor y z 0x000200F3 0x00000012 Descriptor Table 0x00000000

Segmentation in Hardware – 1/7 Segmentation selectors and segmentation resisters 80x86 microprocessors perform address translation in two different ways; real mode and protected mode – the use of real mode is restricted to processor compatibility with older models and OS bootstrap; address translation mainly applies when protected mode is enabled. A logical address consists of two parts: a segment identifier and an offset that specifies the relative address within the given segment. The segment identifier is a 16-bit field called the Segment Selector while the offset is a 32-bit field. The processor provides segmentation registers (cs, ss, ds, es, fs, and gs) whose only purpose is to hold Segment Selectors in order to make it easy to retrieve Segment Selectors quickly. cs: the code segment register pointing to a segment containing program instruction ss: the stack segment register pointing to a segment containing the current program stack ds: the data segment register pointing to a segment containing global and static data The remaining three segmentation registers (es, fs, and gs) are general purpose and may refer to arbitrary data segments. The cs register includes special 2-bit field that specifies the Current Privilege Level (CPL) of the CPU; Linux uses only two of them – 0 for Kernel Mode and 3 for User Mode. Segment descriptor Each segment is represented by an 8-byte Segment Descriptor describing the segment characteristics and stored either in the Global Descriptor Table (GDT) or the Local Descriptor Table (LDT). 15 14 13 12 11 10 9 8 7 6 5 4 3 2 1 Index TI RPL TI : Table Indicator (GDT or LDT) RPL : Requestor Privilege Level

Segmentation in Hardware – 2/7 There is only one GDT defined in the system in general while each process is allowed to have its own LDT if it needs to create additional segments besides those stored in the GDT. The address and size of the GDT in main memory are contained in the gdtr control register, while the address and size of the currently used LDT are contained in the ldtr register. There are several types of segments and thus several types of segment descriptors: Code segment descriptor Indicates that the segment descriptor refers to a code segment; it may be included either in the GDT or in the LDT. The descriptor has the S flag set (= 1, non-system segment). Data segment descriptor Indicates that the segment descriptor refers to a data segment; it may be included either in the GDT or in the LDT. The descriptor has the S flag set (= 1). Stack segments are implemented by means of generic data segments. Task state segment descriptor (TSSD) Indicates that the segment descriptor refers to a task stage segment (TSS) – that is, a segment used to save the contents of the processor registers; it can appear only in the GDT. The corresponding Type field has the value 11 or 9, depending on whether the corresponding process is currently executing on a CPU. The S flag of such descriptors is set to 0. Local Descriptor table descriptor (LDTD) Indicates that the segment descriptor refers to a segment containing an LDT; it can appear only in the GDT. The corresponding Type field has the value 2. The S flag of such descriptors is set to 0.

Segmentation in Hardware – 3/7 Data Segment Descriptor 63 62 61 60 59 58 57 56 55 54 53 52 51 50 49 48 47 46 45 44 43 42 41 40 39 38 37 36 35 34 33 32 BASE(24~31) G B O AVL LIMIT(16~19) 1 DPL S=1 TYPE BASE(16~23) BASE(0~15) LIMIT(0~15) 31 30 29 28 27 26 25 24 23 22 21 20 19 18 17 16 15 14 13 12 11 10 9 8 7 6 5 4 3 2 Code Segment Descriptor 63 62 61 60 59 58 57 56 55 54 53 52 51 50 49 48 47 46 45 44 43 42 41 40 39 38 37 36 35 34 33 32 BASE(24~31) G D O AVL LIMIT(16~19) 1 DPL S=1 TYPE BASE(16~23) BASE(0~15) LIMIT(0~15) 31 30 29 28 27 26 25 24 23 22 21 20 19 18 17 16 15 14 13 12 11 10 9 8 7 6 5 4 3 2 System Segment Descriptor 63 62 61 60 59 58 57 56 55 54 53 52 51 50 49 48 47 46 45 44 43 42 41 40 39 38 37 36 35 34 33 32 BASE(24~31) G O LIMIT(16~19) 1 DPL S=0 TYPE BASE(16~23) BASE(0~15) LIMIT(0~15) 31 30 29 28 27 26 25 24 23 22 21 20 19 18 17 16 15 14 13 12 11 10 9 8 7 6 5 4 3 2

Segmentation in Hardware – 4/7 Segment Descriptor Fields Field Description BASE Contains the linear address of the first byte of the segment G Granularity flag: it if is cleared (equal to 0), the segment size is expressed in bytes; it is expressed in multiples of 4096 bytes. LIMIT Holds the offset of the last memory cell in the segment, thus binding the segment length. When G is set to 0, the size of a segment may vary between 1 byte and 1 MB; otherwise, it may vary between 4KB and 4GB. S System flag: if it is cleared, the segment is a system segment that stores critical data structures such as the Local Descriptor Table; otherwise, it is a normal code or data segment. TYPE Characterizes the segment type and its access rights. DPL Descriptor Privilege Level: used to restrict accesses to the segment. It represents the minimal CPU privilege level requested for accessing the segment. Therefore, a segment with its DPL set to 0 is accessible only when the CPL is 0 – that is, in Kernel Mode – while a segment with its DPL set to 3 is accessible with every CPL value. P Segment-present flag: is equal to 0 if the segment is not stored currently in main memory. Linux always sets this flag (bit 47) to 1, because it never swaps out whole segments to disk. D or B Called D or B depending on whether the segment contains code or data. Its meaning is slightly different in two cases, but it is basically set (equal to 1) if the addresses used as segment offsets are 32 bit long, and it is cleared if they are 16 bits long. AVL May be used by the operating system, but it is ignored by Linux

Segmentation in Hardware – 5/7 Fast access to segment descriptors Logical addresses consist of a 16-bit segment selector and a 32-bit offset and segmentation registers store only the segment selector. In order to speed up the translation of logical addresses into linear addresses, the 80x86 processor provides an additional nonprogrammable register – that is, a register that can not be set by a programmer – for each of the six programmable segmentation registers. Each nonprogrammable register contains the 8-bytes segment descriptor specified by the segment selector contained in the corresponding segmentation register. Every time a segment selector is loaded in a segmentation register, the corresponding segment descriptor is loaded from memory into the matching nonprogrammable CPU register. From then on, translations of logical addresses referring to that segment can be performed without accessing the GDT or LDT stored in main memory; the processor can refer only directly to the CPU register containing the segment descriptor. Accesses to the GDT or LDT are necessary only when the contents of the segmentation registers change. Descriptor Table … Segment Descriptor Virtual Memory … Segment Nonprogrammable Register Segment Descriptor Segmentation Register Segment Selector

Segmentation in Hardware – 6/7 Any segment selector includes three fields : Index, TI and RPL. Because a segment descriptor is 8-byte long, its relative address inside the GDT or the LDT is obtained by multiplying the 13-bit index field of the segment selector by 8. For instance, if the GDT is at 0x00020000 (the value stored in the gdtr register) and the index specified by the segment selector is 2, the address of the corresponding segment descriptor is 0x00020000 + (2  8) or 0x00020010 The first entry of the GDT is always set to 0 in order to ensure that logical addresses with a null segment selector will be considered invalid causing a processor exception. The maximum number of segment descriptors that can be stored in the GDT is 8,191 (213 – 1). Segmentation unit The segmentation unit translates a logical address into a corresponding linear address by: Examines the TI filed of the segment selector to determine which descriptor table stores the segment descriptor. This filed indicates that the descriptor is either in the GDT (in which case the segmentation unit gets the base linear address of the GDT from the gdtr register) or in the active LDT (in which case the segmentation unit gets the base linear address of the LDT from the ldtr register). Computes the address of the segment descriptor from the index field of the segment selector. The index field is multiplied by 8 (the size of a segment descriptor), and the result is added to the content of the gdtr or ldtr register. Adds the offset of the logical address to the Base field of the segment descriptor, thus obtaining the linear address. Notice that the first two operations need to be performed only when a segmentation register has been changed, thanks to the nonprogrammable registers associated with the segmentation registers.

Segmentation in Hardware – 7/7 Segment Selector Fields Field Description Index Identifies the segment descriptor entry contained in the GDT or in the LDT TI Table Indicator: specifies whether the segment descriptor is included in the GDT (TI = 0) or in the LDT (TI = 1) RPL Request Privilege Level: specifies the Current Privilege Level of the CPU when the corresponding segment selector is loaded into the cs register; it also may be used to selectively weaken the processor privilege level when accessing data segments. Logical Address Linear Address INDEX TI Offset + gdtr or ldtr BASE  Segment Descriptor 8 Non-programmable Register … Segment Descriptor Descriptor Table (GDT or LDT) Hardware Segmentation Unit

Segmentation in Linux – 1/5 Segmentation has been included in 80x86 microprocessors to encourage programmers to split their applications into logically related entities, such as subroutines or global and local data areas. Linux uses segmentation in a very limited way; Linux prefers paging (to be discussed later) to segmentation for the following reasons: Memory management is simpler when processes use the same segment register values – that is, when they share the same set of linear addresses. One of the design objectives of Linux is portability to a wide range of architectures; RISC architectures in particular have limited support for segmentation. The 2.6 version of Linux uses segmentation only when required by the 80x86 architecture. All Linux processes running in User Mode use the same pair of segments to address instructions and data, which are called user code segment and user data segment. Similarly, all Linux processes running in Kernel Mode use the same pair of segments to address instructions and data, called kernel code segment and kernel data segment. Values of the Segment Descriptor Fields for the Four Main Linux Segments Segment Base G Limit S Type DPL D/B P User Code 0x00000000 1 0xffff 10 3 User Data 2 Kernel Code 20 Kernel Data

Segmentation in Linux – 2/5 The corresponding segment selectors are defined by the macro __USER_CS, __USER_DS, __KERNEL_CS and __KERNEL_DS, respectively. For instance, the kernel just loads the value yielded by the __KERNEL_CS macro into the cs segmentation register to address the kernel code segment. Notice that the linear addresses associated with such segments starts at 0 and reach the addressing limit of 232-1 implying that all processes, either in User Mode or Kernel Mode, may use the same logical addresses. Another important consequence of having all segments started at 0x00000000 is that logical addresses coincide with linear addresses in Linux; that is, the value of the offset field of a logical address always coincides with the value of the corresponding linear address. The Current Privilege Level of the CPU indicates whether the processor is in User or Kernel Mode and is specified by the RPL field of the segment selector stored in the cs register. Whenever the CPL is changed, some segmentation registers must be correspondingly updated: When the CPL is equal to 3 (User Mode), the ds register must contain the segment selector of the user data segment. When the CPL is equal to 0 (Kernel Mode), the ds register must contain the segment selector of the kernel data segment. The same requirement applies to ss register as well. The Linux GDT There is only one GDT in uniprocessor systems and there is one GDT for each and every CPU in the system in multiprocessor systems. All GDTs are stored in the cpu_gdt_table array and the addresses and sizes of the GDTs (used when initializing the gdtr registers) are stored in the cpu_gdt_descry array.

Segmentation in Linux – 3/5 Linux GDT Linux GDT Null Reserved Not Used TLS #1 TLS #2 TLS #3 Kernel Code Kernel Data User Code User Data TSS LDT PNPBIOS 32-bit Code PNPBIOS 16-bit Code PNPBIOS 32-bit Data PNPBIOS 16-bit Data APMBIOS 32-bit Code APMBIOS 16-bit Code APMBIOS Data Not Used Double Fault TSS 0x80 0x88 0x90 0x98 0xa0 0xa8 0x33 0xb0 0x3b 0xb8 0x43 0xc0 0xc8 0x7b (__KERNEL_CS) 0x7b (__KERNEL_DS) 0x7b (__USER_CS) 0x7b (__USER_DS) 0x7b Each GDT includes 18 segment descriptors and 14 null, unused or reserved entries. Unused entries are inserted on purpose so that segment descriptors usually accessed together are kept in the same 32-byte line of the hardware caches. The 18 segment descriptors included in each GDT point to the following segments: 4 user/kernel code/data segments A task state segment (TSS) – different for each processor in the system A default local descriptor table segment (LDT), usually shared by all processes 3 thread local storage (TLS) segments

Segmentation in Linux – 4/5 3 segments related to advanced power management (APM) 5 segments related to plug and play BIOS (PNP) services A special TSS segment used by the kernel to handle “Double Fault” exceptions Each copy of GDT for each processor stores identical entries except for a few case: Each processor has its own TSS segment, thus the corresponding GDT entries differ. A few entries in the GDT may depend on the process that the CPU is executing (LDT and TLS). A processor may temporarily modify an entry in its copy of the GDT in some cases; this happens when invoking and APM BIOS procedure, for instance. The Linux LDTs Most Linux User Mode applications do not make use of a local descriptor table, thus the kernel defines a default LDT to be shared by most processes. The default LDT is stored in the default_ldt array which includes 5 entries while only 2 of them are effectively used by the kernel – a call gate for iBCS executables and a call gate for Solaris/x86 executables. Call gates are a mechanism provided by 80x86 microprocessors to change the current privilege level of the CPU while invoking a predefined functions. Processes may require to set up their own LDT in some cases, which turns out to be useful to applications (such as Wine) that execute segment-oriented Microsoft Windows applications. The modify_ldt( ) system call allows a process to set up its own LDT. Any custom LDT created by modify_ldt( ) also requires its own segment – when a processor starts executing a process having a custom LDT , the LDT entry in the CPU-specific copy of the GDT is changed accordingly.

Segmentation in Linux – 5/5 User mode applications also may allocate new segments by means of modify_ldt( ); however, the kernel never makes use of these segments and it does not have to keep track of the corresponding segment descriptors because they are included in the custom LDT of the process.

Paging in Hardware – 1/7 Paging unit Paging unit translates linear addresses into physical ones. One key task in the unit is to check the requested access type against the access rights of the linear address – if the memory access is not valid, it generates a page fault exception. Linear addresses are grouped in fixed-length intervals called page for the sake of efficiency; contiguous linear addresses within a page are mapped into contiguous physical addresses. The kernel can specify the physical address and the access rights of a page instead of those of all the linear addresses included in it in this way. The paging unit thinks of all RAM as partitioned into fixed-length page frames (physical pages). Each page frame contains a page – that is, the length of a page frame coincides with that of a page. A page frame is a constituent of main memory and hence it is a storage area. It is important to distinguish a page from a page frame; the former is just a block of data, which may be stored in any page frame or on disk. The data structure that map linear to physical addresses are called page tables; they are stored in main memory and must be properly initialized by the kernel before enabling the paging unit. Starting with the 80386, all 80x86 processors support paging; it is enabled by setting the PG flag of a control register named cr0. When PG = 0, linear addresses are interpreted as physical addresses. Regular paging Starting with the 80386, the paging unit of Intel processors handles 4KB pages. The 32 bits of a linear address are divided into three fields:

Paging in Hardware – 2/7 31 30 29 28 27 26 25 24 23 22 21 20 19 18 17 16 15 14 13 12 11 10 9 8 7 6 5 4 3 2 1 Directory Table Offset Directory: the most significant 10 bits (210 = 1024 = 1KB) – the table of page tables Table: the intermediate 10 bits (210 = 1024 = 1KB) – the table of pages Offset: the least significant 12 bits (212 = 4096 = 4KB) – location within the page The translation of linear addresses is accomplished in 2 steps, each based on a type of translation table. The first translation table is called the page directory and the second is called the page table. The purpose of this two-level page table scheme is to reduce the amount of RAM required for per- process page tables – it would require up to 220 entries (4 bytes per entry  4MB of RAM) to represent the page table for each process (assuming each process uses a full 4GB linear address space) even though a process actually does not use all addresses in that range if a simple one-level page table implementation was used. The two-level page table implementation reduces the memory by requiring page tables only for those virtual memory regions actually used by a process. Each active process must have a page directory (1024  4byte = 4KB) assigned to it; however there is no need to allocate RAM for all page tables of a process at once; it is more efficient to allocate RAM for a page table only when the process effectively needs it. The physical address of the page directory in use is stored in a control register named cr3. The directory field within the linear address determines the entry in the page directory that points to the proper page table. The table field within the linear address determines the entry in the page table that contains the physical address of the page frame containing the page.

Paging in Hardware – 3/7 + + + The offset field within the linear address determines the relative position within the page frame which can contain up to 4KB of data. Both the directory and the table fields are 10 bits long and each of page directory and page table can include up to 1,024 entries; it implies that such an addressing implementation can address up to 1024  1024  4094 = 232 memory cells as expected with 32-bit address space. Linear Address 31 22 21 12 11 Directory Table Offset Page … Data + Page Table … Page Frame Page Directory + … Table Index + cr3

Paging in Hardware – 4/7 The entries of page directories and page tables have the same structure: Field Description Present flag If it is set, the referred-to page (or page table) is contained in main memory; if it is cleared, the page is not contained in main memory and the remaining entry bits may be used by the operating system for its own purpose. If the entry of page table or page directory needed to perform address translation has the present flag cleared, the paging unit stores the linear address in a control register named cr2 and generates exception 14 – the page fault exception. 20 MSB of page frame physical address Because each page frame has a 4-KB capacity, its physical address must be a multiple of 4096, so the 12 least significant bits of the physical address are always equal to 0. If the field refers to a page directory, the page frame contains a page table; if it refers to a page table, the page frame contains a page of data. Accessed flag Set each time the paging unit addresses the corresponding page frame. This flag may be used by the operating system when selecting pages to be swapped out. The paging unit never resets this flag; this must be done by the operating system. Dirty flag Applies only to the page table entries. It is set each time a write operation is performed on the page frame. As with the accessed flag, dirty may be used by the operating system when selecting pages to be swapped out. The paging unit never resets this flag; this must be done by the operating system. Read/Write flag Contains the access right (Read/Write or Read) of the page or of the page table. User/Supervisor flag Contains the privilege level required to access the page or page table. PCD and PWT flags Controls the way the page or page table is handled by the hardware cache. Page size flag Applies only to page directories. If it is set, the entry refers to a 2 MB – or 4 MB long page frame. Global flag Applies only to page table entries. This flag was introduced in the Pentium Pro to prevent frequently used pages from being flushed from the TLB cache. It works only if the page global enable (PGE) flag of register cr4 is set.

Paging in Hardware – 5/7 + + Extended paging Starting with the Pentium model, 80x86 microprocessors introduce extended paging, which allows page frames to be 4 MB instead of 4 KB in size. Extended paging is used to translate large contiguous linear address ranges into corresponding physical ones; in these cases, the kernel can do without intermediate page tables and thus save memory and preserve TLB entries. Extended paging is enabled by setting the page size flag of a page directory entry; in this case, the paging unit divides the 32 bits of a linear address into two fields: Directory: the most significant 10 bits Offset: the remaining 22 bits Page directory entries for extended paging are the same as for normal paging except that: The page size flag must be set. Only the 10 most significant bits of the 20-bit physical address field are significant. This is because each physical address is aligned on a 4-MB boundary, so the 22 least significant bits of the address are 0. Extended paging co-exists with regular paging; it is enabled by setting the PSE flag of the cr4 processor register. Linear Address 31 22 21 Directory Offset 4MB Page … Data + Page Directory … Page Frame + cr3

Paging in Hardware – 6/7 Hardware protection scheme Paging unit uses a different protection scheme from the segmentation unit. While 80x86 processors allow four possible privilege levels to a segment, only two (0 or 3) privilege levels are associated with pages and page tables because privileges are controlled by the User/Supervisor flag. When this flag is 0, the page can be addressed only when the CPL is less than 3 (Kernel Mode). When the flag is 1, the page can always be addressed. Instead of the three types of access rights (Read, Write and Execute) associated with segments, only two types of access rights (Read and Write) are associated with pages. If the Read/Write flag of a page directory or a page table entry is equal to 0, the corresponding page table or page can only be read; otherwise, it can be read and written. Example of regular paging Let’s assume that the kernel assigns the linear address space between 0x20000000 and 0x2003ffff to a running process. This space consists of exactly 64 pages and let’s start with the 10 most significant bits of the linear addresses assigned to the process, which are interpreted as the directory field by the paging unit. The addresses start with a 2 followed by zeros, so the 10 bits all have the same value, namely 0x080 or 128 decimal. Thus, the directory field in all the addresses refers to the 129th entry of the process page directory. The corresponding entry must contain the physical address of the page table assigned to the process. If no other linear addresses are assigned to the process, all the remaining 1,023 entries of the page directory are filled with zeros.

Paging in Hardware – 7/7 Page Directory Page Table 1023 (0x3ff) … … 1023 (0x3ff) 128 (0x080) 64 (0x040) 63 (0x03f) 000 (0x000) 00 (0x000) The values assumed by the intermediate 10 bits, (that is, the values of the table field) range from 0 to 0x03f, or from 0 to 63 decimal - thus, only the first 64 entries of the page table are valid. The remaining 960 entries are filled with zeros. Suppose that the process needs to read the byte at linear address 0x20021406 and such an address is handled by the paging unit as follows: The directory field 0x80 is used to select entry 0x80 of the page directory, which points to the page table associated with the process’s pages. The table field 0x21 is used to select entry 0x21 of the page table, which points to the page frame containing the desired page. Finally, the offset field 0x406 is used to select the byte at offset 0x406 in the desired page frame. If the present flag of the 0x21 entry of the page table is cleared, the page is not present in main memory; in this case, the paging unit issues a page fault exception while translating the linear address. The same exception is issued whenever the process attempts to access linear addresses outside of the interval delimited by 0x20000000 and 0x2003ffff, because the page table entries not assigned to the process are filled with zeros; in particular, their present flags are all cleared.

Linux 운영체제 및 응용 End of Lecture Note 오상규 정보통신대학원