Skip to contentSkip to conversation

Arm Cortex M4 floating point

DISCUSSION Mircea Cociuba 3 participants 12 messages Aug 17, 2020 – Aug 19, 2020 1 message not in the archive

lore
Share & tools
lore

A message that is not in the archive (sent off-list or not yet received): [email protected]

#1 lore raw

Hello Guys,
Currently trying to run some code that uses FPU instructions.Arm-CortexM4F core. Using the stelaris board with M3 core, but param M4. qemu-system-arm -M lm3s811evb -cpu cortex-m4 -m 8K-nographic -kernel notmain.bin How can I use the FPU in QEMU?
Br, Mircea

#2 lore raw
Quoted 11 lines · Mircea Cociuba

Hello Guys,

Currently trying to run some code that uses FPU instructions. Arm-CortexM4F core.

Using the stelaris board with M3 core, but param M4.

qemu-system-arm -M lm3s811evb -cpu cortex-m4 -m 8K -nographic -kernel notmain.bin

How can I use the FPU in QEMU?

Try using a machine type that uses the Cortex-M4, like the netduinoplus2. Usually it's a bad idea for arm boards to try to specify a different CPU than the one they default to.

thanks
-- PMM

#3 lore raw

Hello, the netduinoplus2 is not part of the machine list in qemu "QEMU emulator version 4.2.0"in which one is it?in the 4.1.0 version release notes it says "ARM: FPU emulation support for Cortex-M CPUs, FPU fixes for Cortex-R5F" Now, is there an option to be passed to qemu to get the FPU working?

Mircea Cociuba"To be or not to be, that is not a question, it's FF".

    On Tuesday, August 18, 2020, 03:56:31 PM GMT+3, Peter Maydell <[email protected]> wrote:  
Quoted 11 lines · Mircea Cociuba

Hello Guys,

Currently trying to run some code that uses FPU instructions. Arm-CortexM4F core.

Using the stelaris board with M3 core, but param M4.

qemu-system-arm -M lm3s811evb -cpu cortex-m4 -m 8K -nographic -kernel notmain.bin

How can I use the FPU in QEMU?

Try using a machine type that uses the Cortex-M4, like the netduinoplus2. Usually it's a bad idea for arm boards to try to specify a different CPU than the one they default to.

thanks
-- PMM

#4 lore raw
Quoted 6 lines · Mircea Cociuba

Hello, the netduinoplus2 is not part of the machine list in qemu "QEMU emulator version 4.2.0" in which one is it?
in the 4.1.0 version release notes it says "ARM: FPU emulation support for Cortex-M CPUs, FPU fixes for Cortex-R5F"

Now, is there an option to be passed to qemu to get the FPU working?

I think from 4.1.0 we mostly implemented the FPU for the benefit of the Cortex-M33 model; we enabled it in the M4 but I don't know if there are any board models that use it. (You can use the M4 in the linux-user mode also).

You could either use a newer QEMU version, or see if the QEMU you have supports a Cortex-M33 board and if that would be suitable for your needs.

That said, I think looking at the code that the stellaris board will put a Cortex-M4 in the emulated system if you use -cpu cortex-m4, though if you do this you are wandering into unsupported may-or-may-not-work territory. You don't say what actually goes wrong. (In particular, make sure your guest code actually enables the FPU via the relevant system registers before trying to execute fp instructions.)

thanks
-- PMM

#5 lore raw

Yeah well, I am in the middle of the may-or-may-not-work territory, currenlty digging for food.
The best I can do is debug it remotly, and see what happens,here let me show you:
gcc params: -Wall -Werror -O2 -nostdlib -nostartfiles -ffreestanding  -mcpu=cortex-m4 -mhard-float -mfpu=vfpv4-d16 (to make sure I have FPU instructions)
the source code:
void PUT32 ( unsigned int, unsigned int );#define UART0BASE 0x4000C000
int notmain ( void ){    unsigned int rx;            unsigned char s[]="QEMU is not that interesting";      //dummy FPU command     asm("vmov s15, r3"); // If you are to remove this, the qemu would print after I type the command continue in gdb, with this line, it shows nothing        for(rx=0;rx<sizeof(s);rx++)    {        PUT32(UART0BASE+0x00,s[rx]);    }            return(0);} Qemu starts like this :qemu-system-arm -S -s -M lm3s811evb -cpu cortex-m4 -m 8K -nographic -kernel notmain.bin

I connect to the debugger gdb file notmain.elf
i get this "notmain.elf" is not a core dump: file format not recognized" but it still works, see comment above for the asm instruction.
Must I manualy tell qemu that the FPU core is active? like set the registers accordingly? Br, Mircea

Mircea Cociuba"To be or not to be, that is not a question, it's FF".

    On Tuesday, August 18, 2020, 04:28:51 PM GMT+3, Peter Maydell <[email protected]> wrote:  
Quoted 6 lines · Mircea Cociuba

Hello, the netduinoplus2 is not part of the machine list in qemu "QEMU emulator version 4.2.0" in which one is it?
in the 4.1.0 version release notes it says "ARM: FPU emulation support for Cortex-M CPUs, FPU fixes for Cortex-R5F"

Now, is there an option to be passed to qemu to get the FPU working?

I think from 4.1.0 we mostly implemented the FPU for the benefit of the Cortex-M33 model; we enabled it in the M4 but I don't know if there are any board models that use it. (You can use the M4 in the linux-user mode also).

You could either use a newer QEMU version, or see if the QEMU you have supports a Cortex-M33 board and if that would be suitable for your needs.

That said, I think looking at the code that the stellaris board will put a Cortex-M4 in the emulated system if you use -cpu cortex-m4, though if you do this you are wandering into unsupported may-or-may-not-work territory. You don't say what actually goes wrong. (In particular, make sure your guest code actually enables the FPU via the relevant system registers before trying to execute fp instructions.)

thanks
-- PMM

#6 lore raw
Quoted 8 lines · Mircea Cociuba

gcc params: -Wall -Werror -O2 -nostdlib -nostartfiles -ffreestanding -mcpu=cortex-m4 -mhard-float -mfpu=vfpv4-d16 (to make sure I have FPU instructions)

the source code:

void PUT32 ( unsigned int, unsigned int );
#define UART0BASE 0x4000C000

int notmain ( void )

Quoted 8 lines
{
    unsigned int rx;


    unsigned char s[]="QEMU is not that interesting";

//dummy FPU command
asm("vmov s15, r3"); // If you are to remove this, the qemu would print after I type the command continue in gdb, with this line, it shows nothing

This looks like you're trying to use the FPU, but you haven't enabled it. If you're writing bare metal code then it is the job of your guest code to make sure it enables the FPU first, exactly as it has to do on real hardware. Check the Arm architecture reference manual for the details of how to do this (I think the main thing is writing to the CPACR to enable the FPU).

Otherwise QEMU will correctly emulate the behaviour you get on real h/w if you try to use the FPU when it is disabled:
the CPU takes an exception.

thanks
-- PMM

#7 lore raw

Thank you very much Peter,I will try this first thing in the morning,
But before I try this, if the register is memory mapped, the stelaris memory map has no FPU.Is the FPU always at core level at the same address? Br, Mircea

Mircea Cociuba"To be or not to be, that is not a question, it's FF".

    On Tuesday, August 18, 2020, 05:16:39 PM GMT+3, Peter Maydell <[email protected]> wrote:  
Quoted 12 lines · Mircea Cociuba

gcc params: -Wall -Werror -O2 -nostdlib -nostartfiles -ffreestanding  -mcpu=cortex-m4 -mhard-float -mfpu=vfpv4-d16 (to make sure I have FPU instructions)

the source code:

void PUT32 ( unsigned int, unsigned int );
#define UART0BASE 0x4000C000

int notmain ( void )
{
    unsigned int rx;

Quoted 4 lines

    unsigned char s[]="QEMU is not that interesting";

    //dummy FPU command
    asm("vmov s15, r3"); // If you are to remove this, the qemu would print after I type the command continue in gdb, with this line, it shows nothing

This looks like you're trying to use the FPU, but you haven't enabled it. If you're writing bare metal code then it is the job of your guest code to make sure it enables the FPU first, exactly as it has to do on real hardware. Check the Arm architecture reference manual for the details of how to do this (I think the main thing is writing to the CPACR to enable the FPU).

Otherwise QEMU will correctly emulate the behaviour you get on real h/w if you try to use the FPU when it is disabled:
the CPU takes an exception.

thanks
-- PMM

#8 lore raw
Quoted 6 lines · Mircea Cociuba

Thank you very much Peter,
I will try this first thing in the morning,

But before I try this, if the register is memory mapped, the stelaris memory map has no FPU. Is the FPU always at core level at the same address?

The registers like the CPACR are memory mapped as part of the CPU itself[*]. They're architecturally always in the same place regardless of what board is being used.

[*] in QEMU's code internally we actually implement them inside our NVIC model, which is a slightly odd place to do it, but this isn't visible to the guest which just sees the registers in the right places.

thanks
-- PMM

#9 lore raw
In reply to Peter Maydell

hello, This email will be non qemu related, but close to the original problem.
I managed to get the FPU activation sequence to compile, found it on the ARM comunity site:
    // ARM 100166_0001_00_en S7.3.2    __asm("ldr.w r0, =0xE000ED88"); // CPACR    __asm("ldr r1, [r0]");    __asm("orr r1, r1, 0xF << 20"); // [20:23] enable CP10 and CP11    __asm("str r1, [r0]");
The documentation also specifies that this code must be executed in priviledged mode.There is little to no reference to priviledged mode in the Arm documentation.It briefly specifies that a certain CONTROL register must have a specific bit(nPriv) set to be in privileged mode.Where can I find the description of the is register and memory location? and are there specific instructions that work on it? Mircea Cociuba"To be or not to be, that is not a question, it's FF".

    On Tuesday, August 18, 2020, 06:35:33 PM GMT+3, Peter Maydell <[email protected]> wrote:  
Quoted 12 lines · Mircea Cociuba

gcc params: -Wall -Werror -O2 -nostdlib -nostartfiles -ffreestanding  -mcpu=cortex-m4 -mhard-float -mfpu=vfpv4-d16 (to make sure I have FPU instructions)

the source code:

void PUT32 ( unsigned int, unsigned int );
#define UART0BASE 0x4000C000

int notmain ( void )
{
    unsigned int rx;

Quoted 4 lines

    unsigned char s[]="QEMU is not that interesting";

    //dummy FPU command
    asm("vmov s15, r3"); // If you are to remove this, the qemu would print after I type the command continue in gdb, with this line, it shows nothing

This looks like you're trying to use the FPU, but you haven't enabled it. If you're writing bare metal code then it is the job of your guest code to make sure it enables the FPU first, exactly as it has to do on real hardware. Check the Arm architecture reference manual for the details of how to do this (I think the main thing is writing to the CPACR to enable the FPU).

Otherwise QEMU will correctly emulate the behaviour you get on real h/w if you try to use the FPU when it is disabled:
the CPU takes an exception.

thanks
-- PMM

#10 lore raw

The core starts in priviledged mode, most probably you should do this at startup.

Le 19/08/2020 à 13:25, Mircea Cociuba a écrit :

Quoted 68 lines

hello,
This email will be non qemu related, but close to the original problem.

I managed to get the FPU activation sequence to compile, found it on the ARM comunity site:

    // ARM 100166_0001_00_en S7.3.2
    __asm("ldr.w r0, =0xE000ED88"); // CPACR
    __asm("ldr r1, [r0]");
    __asm("orr r1, r1, 0xF << 20"); // [20:23] enable CP10 and CP11 __asm("str r1, [r0]");

The documentation also specifies that this code must be executed in priviledged mode.
There is little to no reference to priviledged mode in the Arm documentation.
It briefly specifies that a certain CONTROL register must have a specific bit(nPriv) set to be in privileged mode.
Where can I find the description of the is register and memory location? and are there specific instructions that work on it?

/Mircea Cociuba/
/"To be or not to be, that is not a question, it's FF"./

On Tuesday, August 18, 2020, 06:35:33 PM GMT+3, Peter Maydell <[email protected]> wrote:

Quoted 1 line · On Tue, 18 Aug 2020 at 15:02, Mircea Cociuba <[email protected]…

gcc params: -Wall -Werror -O2 -nostdlib -nostartfiles

-ffreestanding  -mcpu=cortex-m4 -mhard-float -mfpu=vfpv4-d16 (to make sure I have FPU instructions)

Quoted 11 lines

the source code:

void PUT32 ( unsigned int, unsigned int );
#define UART0BASE 0x4000C000

int notmain ( void )
{
    unsigned int rx;

Quoted 4 lines

    unsigned char s[]="QEMU is not that interesting";

    //dummy FPU command
    asm("vmov s15, r3"); // If you are to remove this, the qemu would

print after I type the command continue in gdb, with this line, it shows nothing

This looks like you're trying to use the FPU, but you haven't enabled it. If you're writing bare metal code then it is the job of your guest code to make sure it enables the FPU first, exactly as it has to do on real hardware. Check the Arm architecture reference manual for the details of how to do this (I think the main thing is writing to the CPACR to enable the FPU).

Otherwise QEMU will correctly emulate the behaviour you get on real h/w if you try to use the FPU when it is disabled:
the CPU takes an exception.

thanks
-- PMM

Signature · 7 lines
*Vincent Dupaquis*
Software security & Cryptography expert
06 24 58 17 05
/Europarc de Pichaury Bâtiment B8 1330 rue Guillibert de la Lauzière
13290 Aix-en-Provence/

www.trusted-objects.com <http://www.trusted-objects.com>
#11 lore raw
In reply to Mircea Cociuba
Quoted 12 lines · Mircea Cociuba

hello,
This email will be non qemu related, but close to the original problem.

I managed to get the FPU activation sequence to compile, found it on the ARM comunity site:

    // ARM 100166_0001_00_en S7.3.2
    __asm("ldr.w r0, =0xE000ED88"); // CPACR
    __asm("ldr r1, [r0]");
    __asm("orr r1, r1, 0xF << 20"); // [20:23] enable CP10 and CP11
    __asm("str r1, [r0]");
Quoted 4 lines

The documentation also specifies that this code must be executed in priviledged mode.
There is little to no reference to priviledged mode in the Arm documentation.
It briefly specifies that a certain CONTROL register must have a specific bit(nPriv) set to be in privileged mode. Where can I find the description of the is register and memory location? and are there specific instructions that work on it?

All this kind of detail is documented in the M-profile Architecture Reference Manual (definition of privileged mode, what the CONTROL register is, what memory location it is at, etc). As Vincent says, execution starts in privileged mode anyway, so unless your bare-metal code is going to do operating-system like work of switching into unprivileged mode to run tasks and then switching back again, you don't need to change CONTROL.nPRIV.

thanks
-- PMM

#12 lore raw

Hello,I managed to get it to work.Thank you all for your support! Mircea Cociuba"To be or not to be, that is not a question, it's FF".

    On Wednesday, August 19, 2020, 02:58:43 PM GMT+3, Peter Maydell <[email protected]> wrote:  
Quoted 12 lines · Mircea Cociuba

hello,
This email will be non qemu related, but close to the original problem.

I managed to get the FPU activation sequence to compile, found it on the ARM comunity site:

    // ARM 100166_0001_00_en S7.3.2
    __asm("ldr.w r0, =0xE000ED88"); // CPACR
    __asm("ldr r1, [r0]");
    __asm("orr r1, r1, 0xF << 20"); // [20:23] enable CP10 and CP11 __asm("str r1, [r0]");

Quoted 4 lines

The documentation also specifies that this code must be executed in priviledged mode.
There is little to no reference to priviledged mode in the Arm documentation.
It briefly specifies that a certain CONTROL register must have a specific bit(nPriv) set to be in privileged mode. Where can I find the description of the is register and memory location? and are there specific instructions that work on it?

All this kind of detail is documented in the M-profile Architecture Reference Manual (definition of privileged mode, what the CONTROL register is, what memory location it is at, etc). As Vincent says, execution starts in privileged mode anyway, so unless your bare-metal code is going to do operating-system like work of switching into unprivileged mode to run tasks and then switching back again, you don't need to change CONTROL.nPRIV.

thanks
-- PMM