Skip to content

Latest commit

 

History

History
99 lines (64 loc) · 3.27 KB

File metadata and controls

99 lines (64 loc) · 3.27 KB

Code: 01.Kernel32/Source/EntryPoint.s, Utility.[h, c]

Explanation

What does the code do?

First Problem

In my Acer laptop with 16GB memory, OS detects only 2GB, and In Samsung laptop with 8GB memory, OS detects only 3GB.

First Solution

Until now, operating system checks physical memory by manually writing to memory. However, this way is discouraged unless it is really necessary. Before OS is loaded to memory, some region of memory is already reserved by hardware. Because result of accessing and writing at the region is unpredictable, it is recommended to use BIOS service. Among various ways to get the memory map, I used a BIOS service called int 0x15, eax=0xE820.

  1. Sometimes accessing reserved area is allowed, but writing does not work. Or even accessing does not work

  2. Sometimes, accessing or writing at reserved area causes reboot

  3. Sometimes, accessing or writing at reserved area causes page fault or other exceptions

  4. In Mint64OS, memory map is stored at 0x20004 and number of entries is at 0x20000

kCheckTotalRamSize function in Utility.c accesses the memory map entries and gets the total available memory size which does not include reserved regions

assembly and BIOS

  1. test is like cmp. The difference is that test is logical comparison and cmp is arithmetic comparison. Also there is some performance and size difference

    • result of test eax, eax is equal to the result of cmp eax, 0 except the size and internal implementation

    • test does bitwise-and operation and discards result

    • cmp does subtraction operation and discards result

  2. int 0x15 EAX=0xE820 is complicated service. I recommend to read osdev wiki to use the service

  3. jc short .failed, jne short .failed, jmp short .jmpin

    • j* short is short jump. If the relative jump is less than 124 in address, short jump should be used because the size of instruction is only 2 bytes. Otherwise, use near jump or far jump

    • far jump additionally includes segment selector

    • near jump works in the same segment (does not include selector in operand)

  4. jcxz, jecxz, jrcxz is jump if cx or ecx or rcx is zero

  5. jbe is for unsigned number comparison

  6. clc means clear CF flag in EFLAGS

  7. stc means set CF flag in EFLAGS

  8. and and test both does the bitwise-and operation and set some flags

    • test instruction discards result

    • and instruction does not discard result

Reference

  1. Detecting Memory (x86)

  2. Difference between long and short jump

  3. signed and unsigned comparison1

  4. signed and unsigned comparison2

  5. Difference between cmp and test

  6. Difference between test and and