CSCI 264 – Assembly Language – Memory Management – Paging (Chapter 8) Multitasking OS vs. Batch OS Batch OS – computer programs are executed in sequence, one after another Multitasking OS – allows programs to take turns using processor Task switch – task state segment – holds the frozen state of a task which is not currently running. Task state segment includes space for all general registers, and a register CR3. Linux is a multitasking system Linux is protected OS Linux uses paging to implement a memory protection. Paging, Virtual Memory In a computer system that does not support virtual memory, when a program generates a memory address it is placed directly on the memory bus which causes the requested memory location to be accessed. On a computer that supports virtual memory, the address generated by a program goes via a memory management unit (MMU). This unit maps virtual addresses to physical addresses. The virtual memory is divided into pages. The physical memory is divided into page frames. The size of the virtual pages and the page frames are the same size – 4K. The available physical memory is divided into pages: Pages in physical memory are called page frames. Each application (process) “lives” inside 4GB space. This space is divided into pages. 4GB corresponds to 1MB of pages: 4GB/4K = 4*2^30/4*2^10 = 2^20 = 1MB The portion of hard disk used for paging is divided into pages. To translate the virtual address to the physical address the system is using page tables. Page tables are divided into pages as well. Kernel addresses lie in the range from C0000000H to FFFFFFFF ( 3 to 4 GB) User addresses in the range from 0 to BFFFFFFFH ( 0 to 3 GB) The basic idea behind Virtual Memory is that the combined size of the program, data, and stack may exceed the amount of physical memory available for it. The operating system keeps those parts of the program currently in use in Main Memory, and the rest on the disk. Example: program uses the instruction MOV reg, [imm] Program generates the virtual address. Virtual address doesn’t go directly to the memory bus, instead they go to an MMU (Memory Management Unit) that maps the virtual address onto the physical address Sizes Table: 1KB = 2^10 2KB = 2*2^10 3KB = 3*2^10 4KB = 4*2^10 5KB = 5*2^10 6KB = 6* 2^10 7KB = 7*2^10 8 KB = 8* 2^10 9KB 10KB 11KB 12KB 1024 bytes 2048 bytes 3072 bytes 4096 bytes 5120 bytes 6144 bytes 7168 bytes 8192 bytes 9216 bytes 10240 bytes 11264 bytes 12288 bytes Example: Suppose we have a computer that can generate 16 – bit addresses (from 0 to 2^16-1 =2^6*2^10 -1 = 64KB – 1). These are virtual addresses. Suppose that this computer has only 32KB of the physical memory. The virtual address space is divided up onto units called pages – 4KB each page. Corresponding units in the physical memory are called page frames – the same size – 4KB each page frame. In our example we have 16 pages and 8 page frames. (64KB / 4KB = 16, 32KB/4KB = 8) See the picture on the next page to understand the following instructions: MOV reg, [0] Virtual address is 0 , this address is sent to MMU. The MMU sees that this virtual address falls in page 0 ( 0 to 4095 = 4*2^10-1), which according to its mapping is page frame 2 ( 8KB= 8192 to 12287 = 12KB -1).Virtual address 0 is transformed to the physical address 8192 and this address was putted onto the bus. MOV reg, [8192] Virtual address 8192 belongs to virtual page 2 ( 8192 to 12287) and the corresponding page frame is 6 ( 24K – 28K) The physical address is 24K = 24576 MOV reg, [5], virtual address 5 was mapped to the physical address: 8197 Virtual address space The physical address 60K to 64K-1 56K to 60K-1 52K to 56K -1 48K to 52K-1 44K to 48K-1 40K to 44K -1 36K to 40K -1 32K to 36K -1 28K to 32K -1 24K to 28K -1 20K to 24K -1 16K to 20K-1 12K to 16K -1 8K to 12K -1 The physical page frame that x x x x 7 x 5 x x x 3 4 0 6 4K to 8K-1 1 4K to 8K -1 0K to 4K-1 2 8K to 12K -1 No address No address No address No address 28K to 32K -1 No address 20K to 24K-1 No address No address No address 12K to 16K-1 16K to 20K -1 0K to 4K -1 24K to 28K -1 X means that this virtual page was not mapped to the physical space. 64K (virtual space ) > 32K ( physical space) In the actual hardware a Present/absent bit keeps track of which pages are physically present in memory. Instruction MOV reg, [32780] causes the CPU to trap the OS (operating system). This trap is called page fault. 32780 belong to the virtual page 8 ( 32K to 36K -1). This page was not mapped to the physical memory. At this point, the OS picks a little-used page frame and writes it’s contents back to the disk. Then, it fetches the page 8 into the page frame just freed.