OSDev.org

The Place to Start for Operating System Developers
It is currently Wed Apr 24, 2024 6:33 pm

All times are UTC - 6 hours




Post new topic Reply to topic  [ 11 posts ] 
Author Message
 Post subject: How to determine RAM size for aarch64
PostPosted: Sun Oct 11, 2015 3:51 am 
Offline
Member
Member
User avatar

Joined: Thu Jun 13, 2013 8:20 am
Posts: 27
Location: Ukraine
I plan to write a simple OS which can run on both x86_64 and aarch64 CPU architectures. At the current phase I need to write a memory manager so I have to find a reliable way of memory detection. It's not a problem for x86_64 since I can use a memory map provided by GRUB. But I currently have no idea how to detect memory on aarch64.

At the moment I program for generic aarch64-device emulated by QEMU ('-machine virt'). In future I'll decide which concrete device I'll support (the decision will be based on its popularity, maybe it will be the DragonBoard 410c).

So how can I reliably detect memory for aarch64?

P.S. I've found a source code which describes the memory map for QEMU virt-device (http://git.qemu.org/?p=qemu.git;a=blob;f=hw/arm/virt.c;h=1b1cc716add0d85a004bc71cc4ad4f077816a5a3;hb=HEAD#l112). The only thing I need to find is a RAM size.


Top
 Profile  
 
 Post subject: Re: How to determine RAM size for aarch64
PostPosted: Sun Oct 11, 2015 5:38 am 
Offline
Member
Member

Joined: Mon Mar 25, 2013 7:01 pm
Posts: 5144
There is no generic way to detect memory for any particular CPU architecture. You must read the documentation for your target hardware to determine the appropriate detection method.

This also applies to x86: the BIOS and UEFI memory maps are only available on PCs, and won't work on other x86-based hardware.


Top
 Profile  
 
 Post subject: Re: How to determine RAM size for aarch64
PostPosted: Sun Oct 11, 2015 7:30 am 
Offline
Member
Member
User avatar

Joined: Thu Jun 13, 2013 8:20 am
Posts: 27
Location: Ukraine
There is a Linux port running on qemu-system-aarch64 (-machine virt). How does it check the size of available RAM?


Top
 Profile  
 
 Post subject: Re: How to determine RAM size for aarch64
PostPosted: Sun Oct 11, 2015 11:19 am 
Offline
Member
Member

Joined: Mon Apr 09, 2007 12:10 pm
Posts: 775
Location: London, UK
ARM32 uses ATAGs to convey information regarding memory ranges to Linux kernels. This has been superseded in ARM64 by the use of the 'flattened device tree', however alternatives also exist including UEFI and ACPI implementations for ARM64 and the fallback method of pre-programmed memory ranges in your bootloader which are defined per-board. I believe the qemu virt machine implementation uses these with the fdt address in R2. You can see it building the fdt in qemu/hw/arm/virt.c.

Regards,
John.

_________________
Tysos | rpi-boot


Top
 Profile  
 
 Post subject: Re: How to determine RAM size for aarch64
PostPosted: Sun Oct 11, 2015 1:15 pm 
Offline
Member
Member
User avatar

Joined: Thu Jun 13, 2013 8:20 am
Posts: 27
Location: Ukraine
Unfortunately all the registers seem to be cleared after startup.


Top
 Profile  
 
 Post subject: Re: How to determine RAM size for aarch64
PostPosted: Sun Oct 11, 2015 5:01 pm 
Offline
Member
Member

Joined: Tue Nov 08, 2011 11:35 am
Posts: 453
AFAIK, QEmu passes ATAGs via registers only when you boot the kernel via "-kernel" argument. On real hardware some bootloader (such as U-Boot) prepares this information and passes to kernel in the same way.


Top
 Profile  
 
 Post subject: Re: How to determine RAM size for aarch64
PostPosted: Mon Oct 12, 2015 4:40 am 
Offline
Member
Member
User avatar

Joined: Thu Jun 13, 2013 8:20 am
Posts: 27
Location: Ukraine
I use '-kernel' option, but all the registers are cleared during startup.

P.S. Here is my code: https://github.com/ababo/arwen


Top
 Profile  
 
 Post subject: Re: How to determine RAM size for aarch64
PostPosted: Mon Oct 12, 2015 8:39 am 
Offline
Member
Member

Joined: Mon Apr 09, 2007 12:10 pm
Posts: 775
Location: London, UK
Nable wrote:
AFAIK, QEmu passes ATAGs via registers only when you boot the kernel via "-kernel" argument. On real hardware some bootloader (such as U-Boot) prepares this information and passes to kernel in the same way.
Thanks, yes, the -kernel option is indeed required to pass the fdt information.

ababo wrote:
I use '-kernel' option, but all the registers are cleared during startup.
I've just tried a stub kernel (simply a continuous loop) on qemu-system-aarch64 2.4.50. If you use the monitor, I get R0=0, R1=ffffffff, R2=44000000, and if you examine 0x44000000 (xp /4bx 0x44000000) you indeed get the fdt magic number (0xd0 0x0d 0xfe 0xed).

ababo wrote:
P.S. Here is my code: https://github.com/ababo/arwen
I can't quite see in your arm64 code where you are preserving (or even testing) R2 prior to jumping to the Rust code (which presumably trashes it somewhere).

Regards,
John.

_________________
Tysos | rpi-boot


Top
 Profile  
 
 Post subject: Re: How to determine RAM size for aarch64
PostPosted: Wed Oct 14, 2015 2:10 am 
Offline
Member
Member
User avatar

Joined: Thu Jun 13, 2013 8:20 am
Posts: 27
Location: Ukraine
Very simple to verify: just add the following line after __start label:

Code:
CBNZ x2, __halt

and you'll see that the x2 register is cleared before startup.

I believe that it works for ARM32, but my question is about ARM64.


Top
 Profile  
 
 Post subject: Re: How to determine RAM size for aarch64
PostPosted: Wed Oct 14, 2015 5:11 am 
Offline
Member
Member

Joined: Tue Nov 08, 2011 11:35 am
Posts: 453
I've tried searching for aarch64 registers and this is what I've found:
https://www.kernel.org/doc/Documentatio ... ooting.txt
Code:
- Primary CPU general-purpose register settings
  x0 = physical address of device tree blob (dtb) in system RAM.
  x1 = 0 (reserved for future use)
  x2 = 0 (reserved for future use)
  x3 = 0 (reserved for future use)

It looks like ATAGs are deprecated on aarch64 and it's time to learn about DTB. I didn't use it before, sorry.

Note that bootloader does something with secondary CPUs (if they are present), you can find it in the end of booting.txt.
Code:
- Secondary CPU general-purpose register settings
  x0 = 0 (reserved for future use)
  x1 = 0 (reserved for future use)
  x2 = 0 (reserved for future use)
  x3 = 0 (reserved for future use)


Top
 Profile  
 
 Post subject: Re: How to determine RAM size for aarch64
PostPosted: Wed Oct 14, 2015 5:12 am 
Offline
Member
Member

Joined: Mon Apr 09, 2007 12:10 pm
Posts: 775
Location: London, UK
ababo wrote:
and you'll see that the x2 register is cleared before startup
It only provides the information if it thinks your kernel is Linux. Using an ELF kernel in particular will cause it to omit setting the registers. The simplest way is probably to use a uboot format using their mkimage tool.

Regards,
John.

_________________
Tysos | rpi-boot


Top
 Profile  
 
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 11 posts ] 

All times are UTC - 6 hours


Who is online

Users browsing this forum: Bing [Bot], Majestic-12 [Bot] and 207 guests


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 post attachments in this forum

Search for:
Jump to:  
Powered by phpBB © 2000, 2002, 2005, 2007 phpBB Group