Code from xv6's bootmain.c:
// Read 'count' bytes at 'offset' from kernel into physical address 'pa'.
// Might copy more than asked.
void
readseg(uchar* pa, uint count, uint offset)
{
uchar* epa;
epa = pa + count;
// Round down to sector boundary.
pa -= offset % SECTSIZE;
// Translate from bytes to sectors; kernel starts at sector 1.
offset = (offset / SECTSIZE) + 1;
// If this is too slow, we could read lots of sectors at a time.
// We'd write more to memory than asked, but it doesn't matter --
// we load in increasing order.
for(; pa < epa; pa += SECTSIZE, offset++)
readsect(pa, offset);
}
I dont understand the following statement:
pa -= offset % SECTSIZE;
What exactly does "Round down to sector boundary" mean ? I don't understand why we are subtracting the physical address (when offset is not zero).
For simplicity, let's say pa = 100 (decimal), count = 50, offset = 5, SECTSIZE = 100.
Then epa becomes 100 + (50*1) = 150.
new pa = 100 - (5%100) = 95. (What's the point ?)
The loop runs 3 times (1 sector more than necessary, why it doesn't matter ?)