diff --git a/sys/amd64/amd64/machdep.c b/sys/amd64/amd64/machdep.c --- a/sys/amd64/amd64/machdep.c +++ b/sys/amd64/amd64/machdep.c @@ -645,7 +645,7 @@ * NB: physmap_idx points to the next free slot. */ insert_idx = physmap_idx; - for (i = 0; i <= physmap_idx; i += 2) { + for (i = 0; i < physmap_idx; i += 2) { if (base < physmap[i + 1]) { if (base + length <= physmap[i]) { insert_idx = i; @@ -659,7 +659,7 @@ } /* See if we can prepend to the next entry. */ - if (insert_idx <= physmap_idx && base + length == physmap[insert_idx]) { + if (insert_idx < physmap_idx && base + length == physmap[insert_idx]) { physmap[insert_idx] = base; return (1); } @@ -670,8 +670,6 @@ return (1); } - physmap_idx += 2; - *physmap_idxp = physmap_idx; if (physmap_idx == PHYS_AVAIL_ENTRIES) { printf( "Too many segments in the physical address map, giving up\n"); @@ -682,11 +680,14 @@ * Move the last 'N' entries down to make room for the new * entry if needed. */ - for (i = (physmap_idx - 2); i > insert_idx; i -= 2) { + for (i = physmap_idx; i > insert_idx; i -= 2) { physmap[i] = physmap[i - 2]; physmap[i + 1] = physmap[i - 1]; } + physmap_idx += 2; + *physmap_idxp = physmap_idx; + /* Insert the new entry. */ physmap[insert_idx] = base; physmap[insert_idx + 1] = base + length;