Getting Approximately Memory Size in x86 Protected Mode without Memory Map: Tech Support Guide
In this guide, we will cover how to get an approximate memory size in x86 protected mode without using a memory map. This is particularly useful in 32-bit protected mode, where the Global Descriptor Table (GDT) base limit is set to 4GB. We will discuss the key concepts involved, including segmentation and accessing memory beyond the first 1MB.
Understanding Protected Mode
Protected mode is a memory management scheme used in x86-based computers. It allows for a more efficient use of memory by providing a larger address space and the ability to protect different parts of memory from being accessed by different programs. In protected mode, the CPU uses a GDT to define the limits and access rights for different segments of memory.
Segmentation and Memory Access
Segmentation is a memory management technique used in protected mode to divide memory into segments. Each segment has its own base address and limit, which define the range of memory addresses that can be accessed within that segment. By dividing memory into segments, the CPU can protect different parts of memory from being accessed by different programs.
In order to access memory beyond the first 1MB in protected mode, you need to set up a segment descriptor in the GDT that allows access to the higher memory addresses. This can be done by setting the base address of the segment descriptor to the starting address of the higher memory and setting the limit to the size of the memory you want to access.
Getting the Approximate Memory Size
To get the approximate memory size in protected mode without using a memory map, you can use the following steps:
- Set up a segment descriptor in the GDT that allows access to the higher memory addresses.
- Calculate the end address of the higher memory by adding the size of the higher memory to the starting address.
- Read the value of the CR0 control register to determine if paging is enabled.
- If paging is enabled, disable it by clearing the PG (paging) bit in the CR0 register.
- Calculate the physical address of the end address of the higher memory by adding the value of the PDPT (Page Directory Pointer Table) register to the end address.
- Read the value at the physical address of the end address of the higher memory.
- If the value read is 0xFFFFFFFF, the memory is not present, otherwise it is present.
- Repeat steps 5-7 for each 4GB block of memory until the end of the physical address space is reached.
- Add up the size of all the present memory blocks to get the approximate memory size.
Code Example
Here is an example of how to get the approximate memory size in x86 protected mode without using a memory map:
; Set up a segment descriptor in the GDT that allows access to the higher memory addresses
mov eax, 0x10000
mov ebx, 0xFFFFF
mov ecx, 0x00000000
mov edx, 0x0000FFFF
mov [gdtr + 2], ebx
mov [gdtr], eax
; Calculate the end address of the higher memory
mov eax, 0x100000
mov ebx, 0x100000000
sub ebx, eax
mov ecx, ebx
; Read the value of the CR0 control register
mov eax, cr0
test eax, 0x80000000
jz no\_paging
; Disable paging
mov eax, cr0
and eax, 0x7FFFFFFF
mov cr0, eax
no\_paging:
; Calculate the physical address of the end address of the higher memory
mov eax, cr3
shl eax, 12
add eax, ecx
; Read the value at the physical address of the end address of the higher memory
mov eax, [eax]
; Check if the memory is present
cmp eax, 0xFFFFFFFF
je no\_memory
; Memory is present
add dword [memory\_size], ebx
no\_memory:
; Repeat for each 4GB block of memory
add dword [gdtr + 2], 0x100000
add dword ecx, 0x100000000
cmp ecx, 0x10000000000
jl no\_paging
; Calculate the approximate memory size
mov eax, [memory\_size]
mov edx, 0
mov ebx, 1024
div ebx
mov [memory\_size], eax
In this guide, we have discussed how to get an approximate memory size in x86 protected mode without using a memory map. We have covered the key concepts involved, including segmentation and memory access, and provided a code example to demonstrate the process.
References
- Intel Corporation. (2021). System Programming Guide. https://software.intel.com/content/www/us/en/develop/articles/intel-sdm.html
- Wikipedia. (2021). Protected Mode. https://en.wikipedia.org/wiki/Protected_mode
- Wikipedia. (2021). Segmentation (computing). https://en.wikipedia.org/wiki/Segmentation_(computing)