Raspberry Pi 4

Programming, for all ages and all languages.
zap8600
Member
Member
Posts: 195
Joined: Tue Nov 02, 2021 11:26 am
Libera.chat IRC: zap8600

Raspberry Pi 4

Post by zap8600 »

Hi. I'm making my own OS and would like to port it to Raspberry Pis (currently only the Raspberry Pi 4). Unfortunately, I can't find any information on a Aarch32 Bootloader for the Raspberry Pi 4. I was going to use the bootloader from the OSDev Wiki Page, but I don't know what registers I need to change it to. If you're confused on what "bootloader" I'm talking about, I'm talking about boot.S. Also, how do I make a malloc command? My original OS has a kmalloc, but I'm not sure if the code would work for Aarch32. The reason I need a malloc command is to use the USB drivers made by Chadderz121 on Github. Any information would be helpful.
kzinti
Member
Member
Posts: 898
Joined: Mon Feb 02, 2015 7:11 pm

Re: Raspberry Pi 4

Post by kzinti »

I would just use UEFI to boot on Raspberry Pi 4: https://github.com/pftf/RPi4. Also see https://rpi4-uefi.dev/.

There is also a version for Raspberry Pi 3 here (which I've been using without any problems): https://github.com/pftf/RPi3

If you don't want to use UEFI, I would still look at the source code to understand how to boot on Raspberry Pi 4.
zap8600
Member
Member
Posts: 195
Joined: Tue Nov 02, 2021 11:26 am
Libera.chat IRC: zap8600

Re: Raspberry Pi 4

Post by zap8600 »

I get what you're saying, but I'm trying to make it as Aarch32 code, not Aarch64. I also don't know how to get that UEFI to boot my OS. Also using boot.S would also let me set some things on boot I could want.
Octocontrabass
Member
Member
Posts: 5516
Joined: Mon Mar 25, 2013 7:01 pm

Re: Raspberry Pi 4

Post by Octocontrabass »

zap8600 wrote:Unfortunately, I can't find any information on a Aarch32 Bootloader for the Raspberry Pi 4.
It's very similar to the 32-bit boot stub for the Pi 2. Here's a boot stub that can be assembled to support either Pi 2 or 32-bit Pi 4. You could potentially use the same binary for both, if you don't need the boot stub to do anything model-specific.

Why 32-bit instead of 64-bit?
zap8600 wrote:Also, how do I make a malloc command? My original OS has a kmalloc, but I'm not sure if the code would work for Aarch32.
If your kmalloc doesn't already follow the ABI alignment requirements, you might need to modify it a bit, but it shouldn't require any big changes. The big changes will be the hardware-specific parts of your PMM and VMM.
zap8600
Member
Member
Posts: 195
Joined: Tue Nov 02, 2021 11:26 am
Libera.chat IRC: zap8600

Re: Raspberry Pi 4

Post by zap8600 »

Octocontrabass wrote:
zap8600 wrote:Unfortunately, I can't find any information on a Aarch32 Bootloader for the Raspberry Pi 4.
It's very similar to the 32-bit boot stub for the Pi 2. Here's a boot stub that can be assembled to support either Pi 2 or 32-bit Pi 4. You could potentially use the same binary for both, if you don't need the boot stub to do anything model-specific.
I possibly might need to model-specific things in the future. I won't really use the boot stub you are talking about though, because I want it to be understandable. I am new to this stuff. Can I just compile the Pi 2 boot stub.
Octocontrabass wrote:Why 32-bit instead of 64-bit?
Because my original OS is 32-bit and Aarch32 is closer to that. It is just a preference I have for the less advanced stuff until I get better.
Octocontrabass wrote:
zap8600 wrote:Also, how do I make a malloc command? My original OS has a kmalloc, but I'm not sure if the code would work for Aarch32.
If your kmalloc doesn't already follow the ABI alignment requirements, you might need to modify it a bit, but it shouldn't require any big changes. The big changes will be the hardware-specific parts of your PMM and VMM.
ABI alignment? PMM? VMM? I'm sorry, I'm new to this stuff. You can check out My OS's kmalloc and see if it'll work.
Octocontrabass
Member
Member
Posts: 5516
Joined: Mon Mar 25, 2013 7:01 pm

Re: Raspberry Pi 4

Post by Octocontrabass »

zap8600 wrote:Can I just compile the Pi 2 boot stub.
Probably, yeah.
zap8600 wrote:ABI alignment? PMM? VMM?
The ARM ABI is here. You don't have to read all of it! Your malloc() function needs to return pointers that are appropriately aligned for the type of data that's going to be stored.

This is a good starting point for reading about PMM and VMM.
zap8600 wrote:You can check out My OS's kmalloc and see if it'll work.
You've combined your physical memory allocator and your heap allocator. You could make it work by starting at a different address and fixing it to return properly aligned memory, but you really should throw the whole thing away and write a proper physical memory manager. (And what happens if you run out of memory?)
zap8600
Member
Member
Posts: 195
Joined: Tue Nov 02, 2021 11:26 am
Libera.chat IRC: zap8600

Re: Raspberry Pi 4

Post by zap8600 »

Do you have any tips on making a physical memory manager? Like I said, I'm new to this.
Octocontrabass
Member
Member
Posts: 5516
Joined: Mon Mar 25, 2013 7:01 pm

Re: Raspberry Pi 4

Post by Octocontrabass »

There's some information here that might help. The simplest option is a bitmap, with each bit indicating whether a particular page-sized block of physical memory is free. This should work pretty well for a Raspberry Pi, since RAM is one contiguous block, but the bitmap might be excessively large on x86 since there can be large holes of unusable addresses between usable memory.
zap8600
Member
Member
Posts: 195
Joined: Tue Nov 02, 2021 11:26 am
Libera.chat IRC: zap8600

Re: Raspberry Pi 4

Post by zap8600 »

Well, for me, the bitmap sounds good. How do I get and return addresses? How do I access the RAM? How do I check the pages? Where should the check start?
Octocontrabass
Member
Member
Posts: 5516
Joined: Mon Mar 25, 2013 7:01 pm

Re: Raspberry Pi 4

Post by Octocontrabass »

zap8600 wrote:How do I get and return addresses?
Do you mean from the bitmap? Usually the physical address is something like "(byte_offset * 8 + bit_offset) * PAGE_SIZE".
zap8600 wrote:How do I access the RAM? How do I check the pages? Where should the check start?
I don't understand these questions at all. Aren't you already accessing RAM? What needs to be checked?
zap8600
Member
Member
Posts: 195
Joined: Tue Nov 02, 2021 11:26 am
Libera.chat IRC: zap8600

Re: Raspberry Pi 4

Post by zap8600 »

Hi. So I'm looking at this page on memory managers, and I have some questions. How do I access the RAM? How do I check for the End of Memory status? How do I allocate a page? I have more questions, but I'll go through it little by little.
Octocontrabass
Member
Member
Posts: 5516
Joined: Mon Mar 25, 2013 7:01 pm

Re: Raspberry Pi 4

Post by Octocontrabass »

zap8600 wrote:How do I access the RAM?
What do you mean? You're already accessing RAM. All of your kernel's code and data is in RAM.
zap8600 wrote:How do I check for the End of Memory status?
Read the block header. If it says it's the end, it's the end.
zap8600 wrote:How do I allocate a page?
If you're using paging for memory management, call your virtual memory manager. Your virtual memory manager will call your physical memory manager. Your physical memory manager will locate an available page, mark the page unavailable, and return its physical address to the virtual memory manager. Your virtual memory manager will update the page tables to map that page's physical address at your desired virtual address.

If you're not using paging for memory management, then you don't have to worry about pages. You can allocate memory some other way. Even if you don't use paging for memory management, you must still use paging to enable all of the caches on ARM CPUs.
zap8600
Member
Member
Posts: 195
Joined: Tue Nov 02, 2021 11:26 am
Libera.chat IRC: zap8600

Re: Raspberry Pi 4

Post by zap8600 »

Octocontrabass wrote:
zap8600 wrote:How do I check for the End of Memory status?
Read the block header. If it says it's the end, it's the end.
But how exactly do I read the block header. Do you have an example?
Octocontrabass wrote:
zap8600 wrote:How do I allocate a page?
If you're using paging for memory management, call your virtual memory manager. Your virtual memory manager will call your physical memory manager. Your physical memory manager will locate an available page, mark the page unavailable, and return its physical address to the virtual memory manager. Your virtual memory manager will update the page tables to map that page's physical address at your desired virtual address.
How do I locate these pages? How do I mark them? How I get the address? How do I make a page table? Do you have an example?
Octocontrabass
Member
Member
Posts: 5516
Joined: Mon Mar 25, 2013 7:01 pm

Re: Raspberry Pi 4

Post by Octocontrabass »

zap8600 wrote:But how exactly do I read the block header. Do you have an example?
The block header is part of your allocator. You create the block header, so you get to decide how to read it.
zap8600 wrote:How do I locate these pages?
On many systems, the firmware will tell you where to find memory (e.g. UEFI boot services or a device tree). But if you're only concerned about the Raspberry Pi 4, you can look up a memory map and hardcode the addresses. Keep in mind your kernel will be loaded somewhere in that memory, so you can't blindly assume all memory in the memory map will be available!
zap8600 wrote:How do I mark them?
However you like. For example, if your physical memory manager uses a bitmap to track available memory, you might clear the bit corresponding to the page.
zap8600 wrote:How I get the address?
However you like. For example, if your physical memory manager uses a bitmap to track available memory, you might multiply the bit offset by the page size to get the physical address.
zap8600 wrote:How do I make a page table?
Reserve some memory somewhere (perhaps a special .bss section in your binary?) and fill it out according to the ARMv7-A manual. The wiki has some information that may help you. (In 32-bit mode, ARMv8 CPUs are compatible with ARMv7.)
zap8600 wrote:Do you have an example?
Which part are you looking for an example of?
zap8600
Member
Member
Posts: 195
Joined: Tue Nov 02, 2021 11:26 am
Libera.chat IRC: zap8600

Re: Raspberry Pi 4

Post by zap8600 »

Octocontrabass wrote:
zap8600 wrote:Do you have an example?
Which part are you looking for an example of?
I guess just anything on the subject. Like, how do I make a block header? Or, how do I set up a basic bitmap system? What is an ideal page size? How would I reserve some space in a .bss? How do I even read these values from RAM? How could I setup this?
Post Reply