flat assembler
Message board for the users of flat assembler.

Index > OS Construction > Understand GetMemoryMap function

Goto page Previous  1, 2
Author
Thread Post new topic Reply to topic
Feryno



Joined: 23 Mar 2005
Posts: 509
Location: Czech republic, Slovak republic
Feryno 03 Dec 2020, 21:16
pls try this


Description:
Download
Filename: mem_map.zip
Filesize: 15.17 KB
Downloaded: 667 Time(s)

Post 03 Dec 2020, 21:16
View user's profile Send private message Visit poster's website ICQ Number Reply with quote
Fulgurance



Joined: 27 Nov 2017
Posts: 276
Fulgurance 09 Dec 2020, 23:33
Thanks you, finnaly with your code, I found what I don't understand, it's just because I don't compare with the good value of EFI error, I don't know why, but I just deducted value start to 0(success) and other value or incremented (error 1,error 2... Etc), but no. Its bit mistake who lost me many time

Sorry Feryno, but my English isn't perfect and when I read your previous replies, I don't understood all. It's difficult when we are not English!
Post 09 Dec 2020, 23:33
View user's profile Send private message Reply with quote
Fulgurance



Joined: 27 Nov 2017
Posts: 276
Fulgurance 11 Dec 2020, 23:05
Finally, I ask question (more generic) , but I think I know the response.

Actually with UEFI, all OS developper can make OS and have all minimal drivers services without hardware specifications (I talk about graphics card, network... etc)

I don't like to have mandatory file system, and I don't like all in UEFI, but have access to all of the computer is very good progress than the BIOS
Post 11 Dec 2020, 23:05
View user's profile Send private message Reply with quote
Feryno



Joined: 23 Mar 2005
Posts: 509
Location: Czech republic, Slovak republic
Feryno 12 Dec 2020, 18:54
That's true. I just do not expect that the hardware will run at full capabilities, very likely only in some slower compatibility universal mode. UEFI already supports SMP so you can use all CPUs / CPU cores. Graphics - you can probably write directly into framebuffer. Disks/filesystem you can use UEFI drivers, but again I do not expect full performance. Net cards - the same, they will work, I just do not expect full performance. CPU - you need to add some power management to lower energy consumption. I managed to wake up bootstrap and application CPUs from ACPI S3 sleep state (a part of my hypervisor work where my hypervisor starts earlier than OS on power on PC and also on wakeup from ACPI S3 sleep) - this part is also written in FASM and surprisingly it is only very small code, it was just painful to develop it as debugging at this stage almost impossible so a lot of massive hangs... if you advance your OS development into this stage I can help you with this part (waking up from ACPI S3 sleep state, but prior that you have manage to put your CPUs into this sleep state).
Post 12 Dec 2020, 18:54
View user's profile Send private message Visit poster's website ICQ Number Reply with quote
Fulgurance



Joined: 27 Nov 2017
Posts: 276
Fulgurance 23 Dec 2020, 08:34
Okay, I made a macro for test UEFI status code after a call, and now I done that :

Code:
format pe64 efi
entry Main

section '.text' code readable executable
include "UEFI/UEFI.fasm"

Main:
mov [UEFIHandle],rcx
mov [UEFISystemTable],rdx
push rbp

UEFISimpleTextOutputProtocolClearScreen
UEFIBootServicesGetMemoryMap [MemoryMapSize],[MemoryMap],[MapKey],[DescriptorSize],[DescriptorVersion]
;UEFIGetStatusCode | UEFI RETURN IN MEMORYMAPSIZE THE SIZE NEEDED FOR THE BUFFER
UEFIBootServicesAllocatePool MemoryType.LoaderData,[MemoryMapSize],[AllocatePoolBufferAddress]
;UEFIGetStatusCode | UEFI LOADERDATA IS FOR UEFI APPLICATION
;UEFIBootServicesGetMemoryMap [MemoryMapSize],[AllocatePoolBufferAddress],[MapKey],[DescriptorSize],[DescriptorVersion]
sub rsp,5*8

    mov rdx,[UEFISystemTable]
    mov rbx,[rdx+SystemTable.BootServices]
    mov rax,[rbx+BootServices.GetMemoryMap]
    lea rcx,[MemoryMapSize]
    mov rdx,[AllocatePoolBufferAddress]
    lea r8,[MapKey]
    lea r9,[DescriptorSize]
    lea r10,[DescriptorVersion]
    mov qword[rsp + 8*4],r10
    call rax

    add rsp,5*8
UEFIGetStatusCode

jmp $

section '.data' data readable writable
include "System/SystemMessage.fasm"
UEFIHandle:                 dq ?
UEFISystemTable:            dq ?
MemoryMapSize:              dq ?
MemoryMap:                  dq ?
MapKey:                     dq ?
DescriptorSize:             dq ?
DescriptorVersion:          dd ?
AllocatePoolBufferAddress:  dq ?    


I already checked GetMemoryMap and AllocatePool status, and it's fine, but I have problem when I try to use the allocated pool for GetMemoryMap.(actually the last GetMemoryMap call is a comment because he don't work)

How I can do that? Why this fail?

My macro:

Code:
macro UEFIBootServicesAllocatePages Type,MemoryType,Pages,Memory
{
    sub rsp,0x20

    mov rdx,[UEFISystemTable]
    mov rbx,[rdx+SystemTable.BootServices]
    mov rax,[rbx+BootServices.AllocatePages]
    mov rcx,Type
    mov rdx,MemoryType
    mov r8,Pages
    lea r9,Memory
    call rax

    add rsp,0x20
}

macro UEFIBootServicesFreePages Memory,Pages
{
    sub rsp,0x20

    mov rdx,[UEFISystemTable]
    mov rbx,[rdx+SystemTable.BootServices]
    mov rax,[rbx+BootServices.FreePages]
    mov rcx,Memory
    mov rdx,Pages
    call rax

    add rsp,0x20
}

macro UEFIBootServicesGetMemoryMap MemoryMapSize,MemoryMap,MapKey,DescriptorSize,DescriptorVersion
{
    sub rsp,5*8

    mov rdx,[UEFISystemTable]
    mov rbx,[rdx+SystemTable.BootServices]
    mov rax,[rbx+BootServices.GetMemoryMap]
    lea rcx,MemoryMapSize
    lea rdx,MemoryMap
    lea r8,MapKey
    lea r9,DescriptorSize
    lea r10,DescriptorVersion
    mov qword[rsp + 8*4],r10
    call rax

    add rsp,5*8
}

macro UEFIBootServicesAllocatePool PoolType,Size,Buffer
{
    sub rsp,0x20

    mov rdx,[UEFISystemTable]
    mov rbx,[rdx+SystemTable.BootServices]
    mov rax,[rbx+BootServices.AllocatePool]
    mov rcx,PoolType
    mov rdx,Size
    lea r8,Buffer
    call rax

    add rsp,0x20
}

macro UEFIBootServicesFreePool Buffer
{
    sub rsp,0x20

    mov rdx,[UEFISystemTable]
    mov rbx,[rdx+SystemTable.BootServices]
    mov rax,[rbx+BootServices.FreePool]
    lea rcx,Buffer
    call rax

    add rsp,0x20
}

macro UEFIBootServicesLocateHandle SearchType,Protocol,SearchKey,BufferSize,Buffer
{
    sub rsp,5*8

    mov rdx,[UEFISystemTable]
    mov rbx,[rdx+SystemTable.BootServices]
    mov rax,[rbx+BootServices.LocateHandle]
    mov rcx,SearchType
    lea rdx,Protocol
    lea r8,SearchKey
    lea r9,BufferSize
    lea r10,Buffer
    mov qword[rsp + 8*4],r10
    call rax

    add rsp,5*8
}

macro UEFIBootServicesLocateProtocol Protocol,Registration,Interface
{
    sub rsp,0x20

    mov rdx,[UEFISystemTable]
    mov rbx,[rdx+SystemTable.BootServices]
    mov rax,[rbx+BootServices.LocateProtocol]
    lea rcx,Protocol
    lea rdx,Registration
    lea r8,Interface
    call rax

    add rsp,0x20
}

macro UEFIBootServicesExitBootServices ImageHandle,Mapkey
{
    sub rsp,0x20

    mov rdx,[UEFISystemTable]
    mov rbx,[rdx+SystemTable.BootServices]
    mov rax,[rbx+BootServices.ExitBootServices]
    mov rcx,ImageHandle
    mov rdx,MapKey
    call rax

    add rsp,0x20
}    


I have invalid parameter status code.I thought its because is double pointer, but if I load the address stocked into this label with mov, the same error.

Is it because I allocate pool, the memorymap size change?
Post 23 Dec 2020, 08:34
View user's profile Send private message Reply with quote
Fulgurance



Joined: 27 Nov 2017
Posts: 276
Fulgurance 24 Dec 2020, 08:26
I found the problem, I need to increase the size of the allocated pool. I double the size of the allocated pool, because think after the allocation, the memory map have size doubled.

Is it that?

Finnaly I stop to use macro, I can't have very good adaptation depending on the situation
Post 24 Dec 2020, 08:26
View user's profile Send private message Reply with quote
Fulgurance



Joined: 27 Nov 2017
Posts: 276
Fulgurance 25 Dec 2020, 15:34
It's solved thanks you, I understand how to do now Wink

I have one question, when you exit the boot services, how I can write or read to the RAM ? Because all UEFI fonctions for memory managements isn't available ??
Post 25 Dec 2020, 15:34
View user's profile Send private message Reply with quote
Feryno



Joined: 23 Mar 2005
Posts: 509
Location: Czech republic, Slovak republic
Feryno 26 Dec 2020, 07:58
Your OS should provide memory allocation functions. UEFI mem map uses an identity memory map (virtual address = physical address). Your OS should prepare paging tables, point CR3 to the base of tables (PML4), fill entries in the tables etc. When your OS changes any entry in the map it should execute invlpg instruction with that address to purge TLB which could contain a cached mapping for the virtual address from previous time. Mov to CR3 erases all nonglobal pages from the TLB. Take in mind that even your OS updates its paging tables the CPU for performance reason may look into TLB first and only then consult your paging tables. That's why take care of caching and invalidate caches when changing paging tables. To invalidate one page of virt. address there is the invlpg instruction, for invalidating all nonglobal there is mov to cr3, for invalidating all including global pages there is clearing CR4.PGE (bit 7) and then eventually restoring the PGE bit back
Post 26 Dec 2020, 07:58
View user's profile Send private message Visit poster's website ICQ Number Reply with quote
Fulgurance



Joined: 27 Nov 2017
Posts: 276
Fulgurance 26 Dec 2020, 10:38
Are there any example I can read for that ?
Post 26 Dec 2020, 10:38
View user's profile Send private message Reply with quote
Fulgurance



Joined: 27 Nov 2017
Posts: 276
Fulgurance 26 Dec 2020, 16:06
What website explain UEFI good ? For example, paging is already enabled or I need to enable it ? Is there section into UEFI documentation explain what settings need to be set before exit boot services ?
And if I want to get the size of the RAM
Post 26 Dec 2020, 16:06
View user's profile Send private message Reply with quote
Display posts from previous:
Post new topic Reply to topic

Jump to:  
Goto page Previous  1, 2

< Last Thread | Next Thread >
Forum Rules:
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum
You cannot attach files in this forum
You can download files in this forum


Copyright © 1999-2024, Tomasz Grysztar. Also on GitHub, YouTube.

Website powered by rwasa.