《操作系统》的实验代码。
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

131 lines
5.7 KiB

  1. In this homework, you will use a simple program, which is known as
  2. paging-linear-translate.py, to see if you understand how simple
  3. virtual-to-physical address translation works with linear page tables. To run
  4. the program, remember to either type just the name of the program
  5. (./paging-linear-translate.py) or possibly this (python
  6. paging-linear-translate.py). When you run it with the -h (help) flag, you
  7. see:
  8. Usage: paging-linear-translate.py [options]
  9. Options:
  10. -h, --help show this help message and exit
  11. -s SEED, --seed=SEED the random seed
  12. -a ASIZE, --asize=ASIZE
  13. address space size (e.g., 16, 64k, ...)
  14. -p PSIZE, --physmem=PSIZE
  15. physical memory size (e.g., 16, 64k, ...)
  16. -P PAGESIZE, --pagesize=PAGESIZE
  17. page size (e.g., 4k, 8k, ...)
  18. -n NUM, --addresses=NUM number of virtual addresses to generate
  19. -u USED, --used=USED percent of address space that is used
  20. -v verbose mode
  21. -c compute answers for me
  22. First, run the program without any arguments:
  23. ARG seed 0
  24. ARG address space size 16k
  25. ARG phys mem size 64k
  26. ARG page size 4k
  27. ARG verbose False
  28. The format of the page table is simple:
  29. The high-order (left-most) bit is the VALID bit.
  30. If the bit is 1, the rest of the entry is the PFN.
  31. If the bit is 0, the page is not valid.
  32. Use verbose mode (-v) if you want to print the VPN # by
  33. each entry of the page table.
  34. Page Table (from entry 0 down to the max size)
  35. 0x8000000c
  36. 0x00000000
  37. 0x00000000
  38. 0x80000006
  39. Virtual Address Trace
  40. VA 0: 0x00003229 (decimal: 12841) --> PA or invalid?
  41. VA 1: 0x00001369 (decimal: 4969) --> PA or invalid?
  42. VA 2: 0x00001e80 (decimal: 7808) --> PA or invalid?
  43. VA 3: 0x00002556 (decimal: 9558) --> PA or invalid?
  44. VA 4: 0x00003a1e (decimal: 14878) --> PA or invalid?
  45. For each virtual address, write down the physical address it
  46. translates to OR write down that it is an out-of-bounds
  47. address (e.g., a segmentation fault).
  48. As you can see, what the program provides for you is a page table for a
  49. particular process (remember, in a real system with linear page tables, there
  50. is one page table per process; here we just focus on one process, its address
  51. space, and thus a single page table). The page table tells you, for each
  52. virtual page number (VPN) of the address space, that the virtual page is
  53. mapped to a particular physical frame number (PFN) and thus valid, or not
  54. valid.
  55. The format of the page-table entry is simple: the left-most (high-order) bit
  56. is the valid bit; the remaining bits, if valid is 1, is the PFN.
  57. In the example above, the page table maps VPN 0 to PFN 0xc (decimal 12), VPN 3
  58. to PFN 0x6 (decimal 6), and leaves the other two virtual pages, 1 and 2, as
  59. not valid.
  60. Because the page table is a linear array, what is printed above is a replica
  61. of what you would see in memory if you looked at the bits yourself. However,
  62. it is sometimes easier to use this simulator if you run with the verbose flag
  63. (-v); this flag also prints out the VPN (index) into the page table. From the
  64. example above, run with the -v flag:
  65. Page Table (from entry 0 down to the max size)
  66. [ 0] 0x8000000c
  67. [ 1] 0x00000000
  68. [ 2] 0x00000000
  69. [ 3] 0x80000006
  70. Your job, then, is to use this page table to translate the virtual addresses
  71. given to you in the trace to physical addresses. Let's look at the first one:
  72. VA 0x3229. To translate this virtual address into a physical address, we first
  73. have to break it up into its constituent components: a virtual page number and
  74. an offset. We do this by noting down the size of the address space and the
  75. page size. In this example, the address space is set to 16KB (a very small
  76. address space) and the page size is 4KB. Thus, we know that there are 14 bits
  77. in the virtual address, and that the offset is 12 bits, leaving 2 bits for the
  78. VPN. Thus, with our address 0x3229, which is binary 11 0010 0010 1001, we know
  79. the top two bits specify the VPN. Thus, 0x3229 is on virtual page 3 with an
  80. offset of 0x229.
  81. We next look in the page table to see if VPN 3 is valid and mapped to some
  82. physical frame or invalid, and we see that it is indeed valid (the high bit is
  83. 1) and mapped to physical page 6. Thus, we can form our final physical address
  84. by taking the physical page 6 and adding it onto the offset, as follows:
  85. 0x6000 (the physical page, shifted into the proper spot) OR 0x0229 (the
  86. offset), yielding the final physical address: 0x6229. Thus, we can see that
  87. virtual address 0x3229 translates to physical address 0x6229 in this example.
  88. To see the rest of the solutions (after you have computed them yourself!),
  89. just run with the -c flag (as always):
  90. ...
  91. VA 0: 00003229 (decimal: 12841) --> 00006229 (25129) [VPN 3]
  92. VA 1: 00001369 (decimal: 4969) --> Invalid (VPN 1 not valid)
  93. VA 2: 00001e80 (decimal: 7808) --> Invalid (VPN 1 not valid)
  94. VA 3: 00002556 (decimal: 9558) --> Invalid (VPN 2 not valid)
  95. VA 4: 00003a1e (decimal: 14878) --> 00006a1e (27166) [VPN 3]
  96. Of course, you can change many of these parameters to make more interesting
  97. problems. Run the program with the -h flag to see what options there are:
  98. - The -s flag changes the random seed and thus generates different
  99. page table values as well as different virtual addresses to translate.
  100. - The -a flag changes the size of the address space.
  101. - The -p flag changes the size of physical memory.
  102. - The -P flag changes the size of a page.
  103. - The -n flag can be used to generate more addresses to translate
  104. (instead of the default 5).
  105. - The -u flag changes the fraction of mappings that are valid, from
  106. 0% (-u 0) up to 100% (-u 100). The default is 50, which means
  107. that roughly 1/2 of the pages in the virtual address space will be valid.
  108. - The -v flag prints out the VPN numbers to make your life easier.