previous | start | next

Determining the Segment for a Virtual Address

The user's struct mem_map is data, not text, but that doesn't completely nail down which segment it is in. The user could declare this struct locally in a function or globally at file scope. For the first case, it would be in the stack segment and for the second, the data segment.

As we saw in the example calculation of the physical address, the stack segment may not begin at virtual address 0. The offset from the beginning of the segment is the difference between the virtual address and the beginning virtual address of the segment. So a non-zero beginning segment address would need to be subtracted to get that offset.

Virtual Physical Length
Text 0 0x151A 2
Data 0 0x151C 1
Stack 0x20 0x153C 1

For virtual address 0x00020DE8, for a process with this mem_map, in which segment is this address located? We first have to convert the byte address to clicks. Assuming the click size is 4096 = 212 = 0x1000, dividing by 4096 is the same as right shifting by 12 bits. So 0x00020DE8 is in click 0x0020 or 0x20.

So this address is in the Stack segment, which starts at virtual address 0x20 * click_size = 0x20000.

The offset in the Stack segment is then clearly 0x20DE8 - 0x20000 = 0xDE8.

The Stack segment begins at physical address 0x153C * click_size = 0x153C000, so the physical address of 0x20DE8 is 0x153C000 + 0xDE8 = 0x153CDE8.

This we saw before. But determining which segment to use in this way depends on how Minix 3 manages memory. That is, there are Minix 3 segments and there are Intel segments with hardware protection. As far as Intel is concerned the entire memory for a Minix 3 process is just one Intel segment. The Minix 3 data segment is followed by the gap, which is followed by the stack. The virtual addresses will for adjacent Minix 3 segments will be consecutive because of this.

Suppose we made a mistake and said that 0x00020DE8 was in the Data segment. If we proceed with the translation anyway, we would use 0x0 for the beginning virtual address of the data segment and calculate the offset in the Data segment as 0x20DE8 - 0x0 = 0x20DE8. But the Data segment begins at physical address 0x151C * click_size = 0x151C000. So the calculation would give the physical address of 0x20DE8 as 0x151C000 + 0x20DE8 = 0x153CDE8 - the same as before.



previous | start | next