JacobL
Posts: 76
Joined: Sun Apr 15, 2012 2:23 pm

Pi 2 extra cores operation

Sat Oct 10, 2015 10:55 pm

It was my impression that the extra cores on the Pi 2 were halted at boot, but it seems like they are really just running an endless loop. I recently ported my very simple bootloader to Pi 2, and something very curious happened. It looked like the code had been forked 4 times when I ran it, because the output to the serial console was in 4 copies and the letters were mixed together as if multiple threads were writing to it simultaneously. It looked like my code had somehow been forked on every core, even though it was only meant to run on the first core.

My bootloader basically copies itself to run from address 0x00-0xFF (yes, it is that small) in order to make room for the kernel to be loaded. My theory is now that the extra cores on Pi 2 are actually executing an endless loop somewhere in this address space and that my bootloader overwrites this/these locations. Are there anyone here that knows more about this? I can't find any mention of it anywhere, the only references I can find refer to a control through the mailbox in order to enable a CPU core, but it looks like there might be another way.

User avatar
rpdom
Posts: 15567
Joined: Sun May 06, 2012 5:17 am
Location: Chelmsford, Essex, UK

Re: Pi 2 extra cores operation

Sun Oct 11, 2015 6:31 am

This viewtopic.php?f=63&t=98367&start=375#p697474 may be significant
dom wrote:By default we insert some startup code at address 0x0 - 0xff, which comes from here.
This does switch to non-secure monitor mode, which is required to write to CNTVOFF.

For bare metal I'd have thought you'd want to avoid our startup code (with kernel_old=1) and start your kernel from 0x0

JacobL
Posts: 76
Joined: Sun Apr 15, 2012 2:23 pm

Re: Pi 2 extra cores operation

Sun Oct 11, 2015 7:18 pm

Yes, that is definitely significant. Do you know if I can use kernel_old=1 and still get the ATAGS/dtb loaded? From the comments it looks like I will not be able to play with the TrustZone functionality while having the kernel read boot parameters via ATAGS/dtb in a Linux compatible manner. While TrustZone is not in play right now, I might still want to look into that later.

As for my bootloader, I can put it anywhere in memory as long as there is room for my kernel at 0x8000.

User avatar
rpdom
Posts: 15567
Joined: Sun May 06, 2012 5:17 am
Location: Chelmsford, Essex, UK

Re: Pi 2 extra cores operation

Sun Oct 11, 2015 7:48 pm

JacobL wrote:Yes, that is definitely significant. Do you know if I can use kernel_old=1 and still get the ATAGS/dtb loaded?
I'm afraid I haven't got as far as trying any bare metal code on my Pi 2Bs yet, just the old A/Bs. I do keep an eye on the Bare metal forum for anything important though.

User avatar
Ultibo
Posts: 158
Joined: Wed Sep 30, 2015 10:29 am
Location: Australia
Contact: Website

Re: Pi 2 extra cores operation

Fri Oct 16, 2015 9:08 am

JacobL wrote:From the comments it looks like I will not be able to play with the TrustZone functionality while having the kernel read boot parameters via ATAGS/dtb in a Linux compatible manner. While TrustZone is not in play right now, I might still want to look into that later.
Hi JacobL,

Thought you might be interested to know that you can switch back to secure mode (and therefore play with TrustZone) from the standard RPi2 boot loader without using kernel_old=1.

The trick is to overwrite the vector table that the default boot loader uses and put your own secure monitor handler in place, you can then issue an "smc"instruction to invoke your new handler and it will be able to update the secure config register to turn off the NS bit before returning to your main code. This requires a bit of leg work with invalidating dcache, icache and branch targets but I have it working and you end up back in secure SVC mode.

The latest firmware adds an extra complication because the boot loader leaves the Pi2 in HYP mode but there is a simple bit of code available from Linux that takes care of that, leaving you in non secure SVC mode so you can still switch back to secure SVC.

The good thing is you can still use the ATAGs etc and you don't have to remember to set anything special in config.txt
Ultibo.org | Make something amazing
https://ultibo.org

Threads, multi-core, OpenGL, Camera, FAT, NTFS, TCP/IP, USB and more in 3MB with 2 second boot!

robomon
Posts: 9
Joined: Thu Aug 27, 2015 8:31 pm

Re: Pi 2 extra cores operation

Sun Oct 25, 2015 6:57 am

Initially I was in the impression that all the cores are running in parallel from the address 0x8000.

But after reading a post from the moderator(link below), I came to know that its not the case.
viewtopic.php?p=789655#p789655

So if all your cores started up, be sure which boot files you are using(I hope its the standard firmware files). If you want, I was trying some multi core examples and for me the secondary cores are executing as the moderator mentioned i.e Core 0 will start up and execute. Core 1 to 3 will wait for the address to be present in Core[n]_mailbox3. The moment I write the address to the mailbox, the secondary cores starts to run.

I have a local copy of my boot files(the standard firmware files). You may try with these - https://github.com/jeffreyantony/multip ... er/sd_card

Return to “Bare metal, Assembly language”