您的位置:首页 > 移动开发

System and Device Programming------lab5(paging and swapping)

2016-04-28 06:23 701 查看
http://www.read.seas.harvard.edu/~kohler/class/06f-aos/lab2.html

Virtual, Linear, and Physical Addresses

In x86 terminology, a virtual address is a "segment:offset"-style address before segment translation is performed; a linear address is what you get after segmentation but before page translation; and a physical address is what you finally get after both segmentation and page translation. Be sure you understand the difference between these three types or "levels" of addresses!The JOS kernel tries to use consistent type names for different kinds of address. In particular, the type
uintptr_t
represents virtual addresses, and
physaddr_t
represents physical addresses. Of course, both these types are really just synonyms for 32-bit integers (
uint32_t
), so the compiler won't stop you from assigning one type to another! Every pointer value in JOS should be a virtual address (once paging is set up), since only virtual addresses can be dereferenced. The kernel runs in protected mode too! To summarize:
In Intel's terminology, aninterrupt is a protected control transfer that is caused by an asynchronous event usually external to the processor, such as notification of external device I/O activity. An exception, in contrast, is a protected control transfer caused synchronously by the currently running code, for example due to a divide by zero or an invalid memory access.
PC booting(BIOS)
http://www.read.seas.harvard.edu/~kohler/class/06f-aos/lab1.html




CR0:has a control flag PG,If 1, enable paging and use the CR3 register, else disable paging
CR2:store the address the program attempted to access.
CR3:used to store the address of the top-level structure,enable the processor to translate linear address into physical addresses by locating the page directly and page tables for the current task.
The global directory stays in physical memory(kernel memory,it's not accessible), the middle directory and page table maybe on disk(page fault),but they all are in memory when they are executing.
1.Goal:to check how many pages are loaded into buffer cache, and the page fault:A page fault exception is caused when a process is seeking to access an area of virtual memory that is not mapped to any physical memory, when a write is attempted on a read-only page, when accessing a PTE or PDE with the reserved bit or when permissions are inadequate.
first modify code in laface@VMdebian6:~/SDP/MK/paging/src
Use several functions:monitor_write_hex:hexdecimal
Monitor_write_dec:decimal printing.



Then in laface@VMdebian6:~/SDP/MK/paging,
Execute commands as following
rm hd.img //remove old hd,img
./build.sh //build new kernel
Sudo ../pack.sh 256//execute a script which helps compile and running a custom unix-like os
./qemu.sh hd.img //running the grub and waits the gdb
//needs to use./ which is to execute the commands written in the script
(details of debugging kernel and script meaning in in pdf Tames...)



So know from the result that there are 265 pages loaded in the buffer cache,when try to load next page,found not there so page fault generates(using functions defined in paging.c)
When sometimes find no change after modifying code,can delete main.o
Before exchange frames,need to disable paging,after needs to enable paging,cz it is like lock,to be sure no page fault or other situations may make the process crash or sth.
For question2 to use main1.c,can create hard link:ln -f main1.c main.c,if don't use -f(force:remove destination file),it will complain existing main.c(then now u can find the content of main.c becomes into content of main1.c but don't forget to backup main.c first)
In x86 land, page directories and page tables together provide the mapping between virtual addresses (memory addresses used by applications) and physical addresses (where the memory currently lives physically in RAM, or not).A page is simply a contiguous chunk of memory. x86 (32-bit) supports 3 sizes of pages: 4MB, 2MB, and 4KB, with the latter being the most commonly used in mainstream operating systems. A page table is an array of 1024 * 32-bit entries (conveniently fitting into a single 4KB page). Each entry points to the physical address of a page. Because a single page table is not able to represent the entire address space on its own (1024 entries * 4KB = only 22-bits of address space), we require a second level page table: a page directory. A page directory also consists of 1024 * 32-bit entries (again fitting into a single page), each pointing to a page table. We can see that now 1024 * 1024 * 4KB = 32-bits and with this 3-level structure we are able to map the entire 4GB virtual address space.When the CPU is asked to access a virtual address, it uses the 10 highest order bits (31:22) to index into the page directory table (the base address of which is stored in a special register). The next 10 highest order bits (21:12) are used to index into the page table pointed to by the page directory entry. The lowest 12 order bits (11:0) are finally used to index a byte in the page pointed to by the page table entry.After analysis the code in paging.c, know to swap 2 frames need to use dir->tables[0]->pages[p1].frame,in paging.h can find paging struct which contains frame and one table has 1024 entries so of course page 0,1 are in first table:table[0].
1st write swap code in paging.c



And add function declaration in paging.h
Void swap_fram(u32int p1,u32int p2);
2nd modify code in main.c to store numbers in pages, print addresses and numbers and call swap,finally check later



Can find page 0 and page 1 exchange number

But when increase the page number:while(i<5)->while(i<265)
Strange results:











???



Paging is achieved through the use of the MMU (temporary: article 1, article 2). The MMU is a unit that transforms virtual addresses into physical addresses based on the current page table.This section focuses on the x86 MMU.
On the x86, the MMU maps memory through a series of tables, two to be exact. They are the paging directory, and the paging table.Both tables contain 1024 4byte entries, making them each 4kb. In the page directory, each entry points to a page table. In the page table, each entry points to a physical address that is then mapped to the virtual address found by calculating the offset within the directory and the offset within the table. This can be done as the entire table system represents a linear 4gb virtual memory map.



Page Directory

The topmost paging structure is the page directory. It is essentially an array of page directory entries that take the following form.The page table address field represents the physical address of the page table that manages the four megabytes at that point. Please note that it is very important that this address be 4 KiB aligned. This is needed, due to the fact that the last bits of the 32-bit value are overwritten by access bits and such



In each page table, as it is, there are also 1024 entries. These are called page table entries, and are verysimilar to page directory entries.
Note: Only explanations of the bits unique to the page table are below.The first item, is once again, a 4kb aligned physical address. Unlike previously, however, the address is not that of a page table, but instead a 4kb block of physical memory that is then mapped to that location in the page table and directory.The Global, or 'G' above, flag, if set, prevents the TLB from updating the address in it's cache if CR3 is reset. Note, that the page global enable bit in CR4 must be set to enable this feature.If the Dirty flag ('D') is set, then the page has been written to. This flag is not updated by the CPU, and once set will not unset itself.The 'C' bit is 'D' bit above.

Linux automatically uses all free RAM for buffer cache, http://emieltje.googlecode.com/svn/trunk/Xcore/Trunk/paging.c
(no subject)
To s231720@studenti.polito.itAdd contact Date Today 18:07
<- <<- -> []

ormat the partition with the ext4 file system using the mkfs.ext4 or mke4fs command:

~]# mkfs.ext4 block_device

~]# mke4fs -t ext4 block_device

where block_device is a partition which will contain the ext4 filesystem you wish to create.

disk image /dev/hda
https://sites.google.com/site/4utils/articles/minimal_linux_system/minimal-linux-system-from-scratch
Howto setup a disk using fdisk in shell script
By Saddam Abu Ghaida | Published October 9, 2011
http://unix.stackexchange.com/questions/3192/what-is-meant-by-mounting-a-device-in-linux mount
http://unix.stackexchange.com/questions/3192/what-is-meant-by-mounting-a-device-in-linux
For the people who execute an interactive commands like fdisk and it needs a lot of user input, and you want to script it. Check this out. Below script is just an example, bit you can think of better ways to use this concept just as kickstart files.

The script will create new primary partition and change the filesystem tag to 83 which represents ext3 , it will save the new partition, and updates partition map

#!/bin/bash

cat <<EOF | fdisk /dev/sda
n
p
1

t
83
w
EOF
partprobe
http://www.tldp.org/HOWTO/Unix-and-Internet-Fundamentals-HOWTO/bootup.html the kernel is the central part of most operating systems; it is a bridge between applications and the actual processing done at the hardware level. The kernel's functionality include
*] managing the system's resources (for interlinking of H/w and the S/W i.e. communication)

2] it's a basic component of an OS. that application software must use to perform its function.

3] It avails all these functionality to the system by making the System call.... These are the general routines like DD [device driver programs ]

Working :::

Operating system tasks are done differently by different kernels, depending on their design and Algorithm and sequencing . While kernels execute all the operating system code in the same address space to increase the performance of the system,

The Most Basic and main functions of the system Kernals are

* .1 Process management
* .2 Memory management
* .3 Device management
* .4 System calls

1] Process Mgmt ---- A process defines which memory portions the application can access.How the processing sloud take place is that Preemptive or non preeptive proceesing .

Which process should takes first i.e. Scheduling.

2] Memory mgmt.---- The kernel has full access to the system's memory and must allow processes to safely access this memory as they require it.It has the Paging and virtual addressing. how much memory should be allotted to process. ie. RAM functioning as well as How the data to be stored at HDD.

3] Device Mgmt. --- How the IO devices should be processed .

For example,You double click on the screen, an application ie. {OS} would make a request to the kernel, which would forward the request to its processor, which is then passed from mouse and the action is performed on the screen

In a plug and play system, a device manager performs a scan such as PCI or USB, to detect installed devices with appropriate drivers.

The dia.....

============
Software [Application program, Utilities}
===========
|||||||
===========
Kernel
===========
||||||
===========
hardware
===========
http://www.tldp.org/HOWTO/Unix-and-Internet-Fundamentals-HOWTO/bootup.html http://www.geek.com/chips/difference-between-real-mode-and-protected-mode-574665/
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息