Page 1 of 1

run from 0x00800000 (pi zero w)

Posted: Sun Dec 23, 2018 12:40 am
by moahrs
HI,

I am doing a code, a SO, in c++.. all is everything ok, but the run code loaded in memory its a pain in the neck, not working...

I am loading simple code, in memory location 0x00800000 (inside ram memory of 512MB)...

the code loaded is here:

Code: Select all

00000000 <_start>:
   0:	e92d4010 	push	{r4, lr}
   4:	eb000002 	bl	14 <mainprog>
   8:	e8bd8010 	pop	{r4, pc}
   c:	e12fff1e 	bx	lr

00000010 <dummy>:
  10:	e12fff1e 	bx	lr

00000014 <mainprog>:
  14:	e12fff1e 	bx	lr
Then, have not special, simple start, run a func name mainprog, return and return... the code "BREAK" when i run...

I do the EXACT code, in c function inside de OS, exact, below:

Code: Select all

void newOS::appcod(void)
{
    asm (
        "push {r4, lr};"
        "bl  mainprogappcod;"
        "pop {r4, pc};"
        "bx  lr;"
        "dummyappcod:"
        "bx  lr;"
        "mainprogappcod:"
        "bx  lr;"
    );
}
is the exact code, and generate the same asm arm code from loaded.. I read the memory from loaded code and from code inside OS, and the code is the same...

The code inside OS, run without problem, fine, but the code loaded at 0x00800000, that is the SAME, not work, crash, halt the system... why?

the routine that i am using to call the code loade is below:

Code: Select all

    uint32_t addr = 0x00800000;

    appcod();	// to demonstrate that exact adm code, with the exact arm code works inside OS as function

    addr = (uint32_t)pi_phys_to_user((void*) addr);

    printf("0x%08X\n", addr);	// demonstre that is 0x00800000

    {
        typedef void func_t(void);
        func_t* progrun = (func_t*)(void*) addr;
        progrun();
    }
when i put too addr = 0x8758 (physical addr of appcod function), the call works just fine, but when i put addr = 0x00800000, not works.. why?

EDIT 1: I try to load at other address (like 0x00020000), and the loaded program not work too... crash... what i not uderstand is why the same asm code generating the same arm code, only in the diferent locations, not work... that is insane for me... I worked with 68000, z80, pic (16f, 18f, 32f) and this is not happens, cpu code is cpu code....

EDIT 2: The code that i loading at 0x00800000 is a bin file, just the same code generated in function inside c function, the contens is below:

Code: Select all

1040 2de9 0200 00eb 1080 bde8 1eff 2fe1
1eff 2fe1 1eff 2fe1 6530 a0e3 0130 53e2
fdff ff1a 1eff 2fe1 2f75 7372 2f6c 6962
2f6c 642e 736f 2e31 0000 0000 0000 0000
0000 0000 0000 0000 0000 0000 0000 0000
0000 0000 0000 0000 0300 0100 0000 0000
0100 0000 0200 0000 0000 0000 0000 0000
0000 0000 0400 0000 6000 0000 0500 0000
5c00 0000 0600 0000 3c00 0000 0a00 0000
0100 0000 0b00 0000 1000 0000 1500 0000
0000 0000 fbff ff6f 0000 0008 0000 0000
0000 0000 0000 0000 0000 0000 0000 0000
0000 0000 0000 0000 0000 0000 0000 0000
0000 0000 0000 0000 0000 0000 7400 0000
0000 0000 0000 0000 
plz, help me... ty a lot for help
Moacir Jr.

Re: run from 0x00800000 (pi zero w)

Posted: Sun Dec 23, 2018 5:49 pm
by Arjan
There is a very good C++ bare metal example here -> https://github.com/rsta2/circle

Also I use C++ from my bare metal C implementation -> https://github.com/vanvught/rpidmx512

Re: run from 0x00800000 (pi zero w)

Posted: Sun Dec 23, 2018 8:02 pm
by bzt
Hi,

I'm sure your problem is just partially code related. Probably because you're using the wrong address (with five zeros):
For 32 bit that should be 0x8000 (three zeros), and for 64 bit that's 0x80000 (four zeros). Other than that, you should have an endless loop (e.g.: "halt: b halt"), because there's nowhere to return to from your code, therefore a crash is guaranteed.

Regardless I also would strongly recommend to take a look at rst's brilliant Circle library, specially since you want to use C++, that will be gold for you.

Cheers,
bzt

Re: run from 0x00800000 (pi zero w)

Posted: Sat Jan 19, 2019 9:10 pm
by emilpopa
Hello,

In your small test :
"push {r4, lr};" save work R4 and link register because is destroyed with bl instruction
"bl mainprogappcod;" execute main.....
"pop {r4, pc};" restore R4 and PC(LR saved initial)
"bx lr;" out of sequence ???
"dummyappcod:"
"bx lr;"
"mainprogappcod:"
"bx lr;" return to caller

There is no error where you run a code, if you respect the following:
- you have the access permission for this memory and all the rights
- the memory is not marked as execute never
you can run this code on each address.

Emil

Re: run from 0x00800000 (pi zero w)

Posted: Sun Jan 20, 2019 5:34 am
by LdB
Mods this probably need to move to a linux forum because clearly it requires linux to give execute permissions to the memory block he wants to run the code in which emil correctly notes above.

Re: run from 0x00800000 (pi zero w)

Posted: Sun Jan 20, 2019 10:56 pm
by Ultibo
LdB wrote:
Sun Jan 20, 2019 5:34 am
Mods this probably need to move to a linux forum because clearly it requires linux to give execute permissions to the memory block he wants to run the code in which emil correctly notes above.
Not sure how you reach the conclusion that this requires Linux, if you enable the MMU you can apply whatever access permission you choose in bare metal, including execute never.

Re: run from 0x00800000 (pi zero w)

Posted: Mon Jan 21, 2019 2:36 am
by LdB
Use of the word OS about a half dozen times, mention or code for MMU zero .. so just a wild guess (going with odds since OS not identified) but I am always wrong :-)
I do the EXACT code, in c function inside de OS, exact, below:
I read the memory from loaded code and from code inside OS
The code that i loading at 0x00800000 is a bin file,
etc etc.

Whatever the case the OS must have control of the memory access (given the error) and it is loading a really simple bin file and we can't help him.