《操作系统》的实验代码。
Non puoi selezionare più di 25 argomenti Gli argomenti devono iniziare con una lettera o un numero, possono includere trattini ('-') e possono essere lunghi fino a 35 caratteri.

166 righe
6.9 KiB

  1. This program allows you to see how address translations are performed in a
  2. system with segmentation. The segmentation that this system uses is pretty
  3. simple: an address space has just *two* segments; further, the top bit of the
  4. virtual address generated by the process determines which segment the address
  5. is in: 0 for segment 0 (where, say, code and the heap would reside) and 1 for
  6. segment 1 (where the stack lives). Segment 0 grows in a positive direction
  7. (towards higher addresses), whereas segment 1 grows in the negative direction.
  8. Visually, the address space looks like this:
  9. --------------- virtual address 0
  10. | seg0 |
  11. | |
  12. | |
  13. |-------------|
  14. | |
  15. | |
  16. | |
  17. | |
  18. |(unallocated)|
  19. | |
  20. | |
  21. | |
  22. |-------------|
  23. | |
  24. | seg1 |
  25. |-------------| virtual address max (size of address space)
  26. With segmentation, as you might recall, there is a base/limit pair of
  27. registers per segment. Thus, in this problem, there are two base/limit
  28. pairs. The segment-0 base tells which physical address the *top* of segment 0
  29. has been placed in physical memory and the limit tells how big the segment is;
  30. the segment-1 base tells where the *bottom* of segment 1 has been placed in
  31. physical memory and the corresponding limit also tells us how big the segment
  32. is (or how far it grows in the negative direction).
  33. As before, there are two steps to running the program to test out your
  34. understanding of segmentation. First, run without the "-c" flag to generate a
  35. set of translations and see if you can correctly perform the address
  36. translations yourself. Then, when done, run with the "-c" flag to check your
  37. answers.
  38. For example, to run with the default flags, type:
  39. prompt> ./segmentation.py
  40. (or
  41. prompt> python ./segmentation.py
  42. if that doesn't work)
  43. You should see this:
  44. ARG seed 0
  45. ARG address space size 1k
  46. ARG phys mem size 16k
  47. Segment register information:
  48. Segment 0 base (grows positive) : 0x00001aea (decimal 6890)
  49. Segment 0 limit : 472
  50. Segment 1 base (grows negative) : 0x00001254 (decimal 4692)
  51. Segment 1 limit : 450
  52. Virtual Address Trace
  53. VA 0: 0x0000020b (decimal: 523) --> PA or segmentation violation?
  54. VA 1: 0x0000019e (decimal: 414) --> PA or segmentation violation?
  55. VA 2: 0x00000322 (decimal: 802) --> PA or segmentation violation?
  56. VA 3: 0x00000136 (decimal: 310) --> PA or segmentation violation?
  57. VA 4: 0x000001e8 (decimal: 488) --> PA or segmentation violation?
  58. For each virtual address, either write down the physical address it translates
  59. to OR write down that it is an out-of-bounds address (a segmentation
  60. violation). For this problem, you should assume a simple address space with
  61. two segments: the top bit of the virtual address can thus be used to check
  62. whether the virtual address is in segment 0 (topbit=0) or segment 1
  63. (topbit=1). Note that the base/limit pairs given to you grow in different
  64. directions, depending on the segment, i.e., segment 0 grows in the positive
  65. direction, whereas segment 1 in the negative.
  66. Then, after you have computed the translations in the virtual address trace,
  67. run the program again with the "-c" flag. You will see the following (not
  68. including the redundant information):
  69. Virtual Address Trace
  70. VA 0: 0x0000020b (decimal: 523) --> SEGMENTATION VIOLATION (SEG1)
  71. VA 1: 0x0000019e (decimal: 414) --> VALID in SEG0: 0x00001c88 (decimal: 7304)
  72. VA 2: 0x00000322 (decimal: 802) --> VALID in SEG1: 0x00001176 (decimal: 4470)
  73. VA 3: 0x00000136 (decimal: 310) --> VALID in SEG0: 0x00001c20 (decimal: 7200)
  74. VA 4: 0x000001e8 (decimal: 488) --> SEGMENTATION VIOLATION (SEG0)
  75. As you can see, with -c, the program translates the addresses for you, and
  76. hence you can check if you understand how a system using segmentation
  77. translates addresses.
  78. Of course, there are some parameters you can use to give yourself different
  79. problems. One particularly important parameter is the -s or -seed parameter,
  80. which lets you generate different problems by passing in a different random
  81. seed. Of course, make sure to use the same random seed when you are generating
  82. a problem and then solving it.
  83. There are also some parameters you can use to play with different-sized
  84. address spaces and physical memories. For example, to experiment with
  85. segmentation in a tiny system, you might type:
  86. prompt> ./segmentation.py -s 100 -a 16 -p 32
  87. ARG seed 0
  88. ARG address space size 16
  89. ARG phys mem size 32
  90. Segment register information:
  91. Segment 0 base (grows positive) : 0x00000018 (decimal 24)
  92. Segment 0 limit : 4
  93. Segment 1 base (grows negative) : 0x00000012 (decimal 18)
  94. Segment 1 limit : 5
  95. Virtual Address Trace
  96. VA 0: 0x0000000c (decimal: 12) --> PA or segmentation violation?
  97. VA 1: 0x00000008 (decimal: 8) --> PA or segmentation violation?
  98. VA 2: 0x00000001 (decimal: 1) --> PA or segmentation violation?
  99. VA 3: 0x00000007 (decimal: 7) --> PA or segmentation violation?
  100. VA 4: 0x00000000 (decimal: 0) --> PA or segmentation violation?
  101. which tells the program to generate virtual addresses for a 16-byte address
  102. space placed somewhere in a 32-byte physical memory. As you can see, the
  103. resulting virtual addresses are tiny (12, 8, 1, 7, and 0). As you can also
  104. see, the program picks tiny base register and limit values, as
  105. appropriate. Run with -c to see the answers.
  106. This example should also show you exactly what each base pair means. For
  107. example, segment 0's base is set to a physical address of 24 (decimal) and is
  108. of size 4 bytes. Thus, *virtual* addresses 0, 1, 2, and 3 are in segment 0 and
  109. valid, and map to physical addresses 24, 25, 26, and 27, respectively.
  110. Slightly more tricky is the negative-direction-growing segment 1. In the tiny
  111. example above, segment 1's base register is set to physical address 18, with a
  112. size of 5 bytes. That means that the *last* five bytes of the virtual address
  113. space, in this case 11, 12, 13, 14, and 15, are valid virtual addresses, and
  114. that they map to physical addresses 13, 14, 15, 16, and 17, respectively.
  115. If that doesn't make sense, read it again -- you will have to make sense of
  116. how this works in order to do any of these problems.
  117. Note you can specify bigger values by tacking a "k", "m", or even "g" onto the
  118. values you pass in with the -a or -p flags, as in "kilobytes", "megabytes",
  119. and "gigabytes". Thus, if you wanted to do some translations with a 1-MB
  120. address space set in a 32-MB physical memory, you might type:
  121. prompt> ./segmentation.py -a 1m -p 32m
  122. If you want to get even more specific, you can set the base register and limit
  123. register values yourself, with the --b0, --l0, --b1, and --l1 registers. Try
  124. them and see.
  125. Finally, you can always run
  126. prompt> ./segmentation.py -h
  127. to get a complete list of flags and options.
  128. Enjoy!