help with powerpc asm (moving grub_ieee1275_entry_fn initialization to crt0.S)
Hi,
For the i386-ieee1275 port, I'd need to move grub_ieee1275_entry_fn initialization in powerpc from cmain.c to crt0.S, so that code in cmain.c can be shared with i386.
The problem is I have no clue about powerpc assembler. On i386, this is just done with a "movl %eax, EXT_C(grub_ieee1275_entry_fn)" but I don't know the equivalent. Furthermore, the code in crt0.S seems to be pushing arguments (up to 3 according to cmain.c) in a loop, which got me really confused.
Anyone who is familiar with powerpc assembler could have a look? See attached patch.
Signature · 5 lines
Robert Millan <GPLv2> I know my rights; I want my phone call! <DRM> What use is a phone call, if you are unable to speak? (as seen on /.)
kern/powerpc/ieee1275/cmain.c +1 -4
@@ -102,12 +102,9 @@ } } -void cmain (uint32_t r3,uint32_t r4,uint32_t r5);void -cmain (UNUSED uint32_t r3, UNUSED uint32_t r4, uint32_t r5) +cmain (void ) { - grub_ieee1275_entry_fn = (int (*)(void *)) r5; -grub_ieee1275_finddevice ("/chosen" , &grub_ieee1275_chosen);grub_ieee1275_find_options ();
kern/powerpc/ieee1275/crt0.S +2 -0
@@ -38,5 +38,7 @@ 2: stwu 2, 4(6) /* We know r2 is already 0 from above. */ bdnz 2b + /* initialize EXT_C(grub_ieee1275_entry_fn) */ + bl cmain 1: b 1b
Here's an incomplete (missing powerpc & sparc) version of the patch that would sanitize this function call.
In the meantime i386 just puts the value in %ecx (third argument) to match what powerpc is doing.
Signature · 5 lines
Robert Millan <GPLv2> I know my rights; I want my phone call! <DRM> What use is a phone call, if you are unable to speak? (as seen on /.)
kern/i386/ieee1275/startup.S +1 -1
@@ -34,5 +34,5 @@ start: _start: - movl %eax, %ecx + movl %eax, EXT_C(grub_ieee1275_entry_fn) jmp EXT_C(cmain)
kern/powerpc/ieee1275/cmain.c +1 -4
@@ -102,12 +102,9 @@ grub_ieee1275_find_options (void) } } -void cmain (uint32_t r3,uint32_t r4,uint32_t r5);void -cmain (UNUSED uint32_t r3, UNUSED uint32_t r4, uint32_t r5) +cmain (void ) { - grub_ieee1275_entry_fn = (int (*)(void *)) r5; -grub_ieee1275_finddevice ("/chosen" , &grub_ieee1275_chosen);grub_ieee1275_find_options ();
Quoting Robert Millan <[email protected]>:
Quoted 2 lines
Here's an incomplete (missing powerpc & sparc) version of the patch that would sanitize this function call.
I don't see why it needs to be done across the board. Maybe you have further simplifications in mind? Anyway, it's a good idea not to rely on C calling conventions on any platform.
Here's the patch for PowerPC. It has been tested both in qemu and on real hardware. I have verified that the value of grub_ieee1275_entry_fn doesn't change.
By the way, please don't remove the declaration of cmain(). It may be useful to prevent a gcc warning (missing declaration). cmain() is really an rare case that doesn't need a declaration, since it's not called by any C code, but gcc doesn't know that.
ChangeLog:
* kern/powerpc/ieee1275/cmain.c (cmain): Don't take any arguments.
* kern/powerpc/ieee1275/crt0.S: Store r5 in grub_ieee1275_entry_fn, don't rely on cmain() doing it.
kern/powerpc/ieee1275/cmain.c +2 -4
@@ -114,12 +114,10 @@ grub_ieee1275_find_options (void) } } -void cmain (uint32_t r3,uint32_t r4,uint32_t r5); +void cmain (void );void -cmain (UNUSED uint32_t r3, UNUSED uint32_t r4, uint32_t r5) +cmain (void ) { - grub_ieee1275_entry_fn = (int (*)(void *)) r5; -grub_ieee1275_finddevice ("/chosen" , &grub_ieee1275_chosen);grub_ieee1275_find_options ();
kern/powerpc/ieee1275/crt0.S +4 -0
@@ -38,5 +38,9 @@ _start: 2: stwu 2, 4(6) /* We know r2 is already 0 from above. */ bdnz 2b + /* Store r5 in grub_ieee1275_entry_fn */ + lis 9, grub_ieee1275_entry_fn@ha + stw 5, grub_ieee1275_entry_fn@l(9) + bl cmain 1: b 1b
Signature · 2 lines
Regards, Pavel Roskin
Quoted 7 lines · Pavel Roskin
Quoting Robert Millan <[email protected]>:
Quoted 2 lines
Here's an incomplete (missing powerpc & sparc) version of the patch that would sanitize this function call.
I don't see why it needs to be done across the board. Maybe you have further simplifications in mind?
What do you mean? The problem I find is that forcing this to be the third parameter of cmain makes it look like cmain has been adapted to accomodate for this particular cpu.
Another option would be to have just one param. This would be the cleanest on i386, since the call from OFW already uses -mregparm=1 for the callback address, but I don't know about powerpc.
Quoted 4 lines
By the way, please don't remove the declaration of
cmain(). It may be useful to prevent a gcc warning (missing declaration).cmain()is really an rare case that doesn't need a declaration, since it's not called by any C code, but gcc doesn't know that.
Ah yes. Right.
kern/powerpc/ieee1275/crt0.S _start: · line 43
Quoted 9 lines
kern/powerpc/ieee1275/crt0.S +3
-0@@ -38,5 +38,9 @@ _start: 2: stwu 2, 4(6) /* We know r2 is already 0 from above. */ bdnz 2b + /* Store r5 in grub_ieee1275_entry_fn */ + lis 9, grub_ieee1275_entry_fn@ha + stw 5, grub_ieee1275_entry_fn@l(9)
I thought you'd also need to remove stuff. Isn't there code above this that pushes the 3 parameters onto the stack?
Signature · 5 lines
Robert Millan <GPLv2> I know my rights; I want my phone call! <DRM> What use is a phone call… if you are unable to speak? (as seen on /.)
Quoted 6 lines · Robert Millan
Quoted 5 lines · Pavel Roskin
Quoting Robert Millan <[email protected]>:
Quoted 2 lines
Here's an incomplete (missing powerpc & sparc) version of the patch that would sanitize this function call.
Quoted 6 lines
Quoted 2 lines
I don't see why it needs to be done across the board. Maybe you have further simplifications in mind?
What do you mean? The problem I find is that forcing this to be the third parameter of cmain makes it look like cmain has been adapted to accomodate for this particular cpu.
Oh, I didn't realize that kern/powerpc/ieee1275/cmain.c is actually used by the i386-ieee1275 build. That's misleading! Perhaps it should be moved to kern/ieee1275/
Anyway, Sparc code doesn't use cmain() in any way. It starts immediately with the function _start() written in C. So you shouldn't worry about breaking Sparc.
Please feel free to integrate the PowerPC part of my patch into your patch, as it indeed needs to be committed at once.
Quoted 3 lines
Another option would be to have just one param. This would be the cleanest on i386, since the call from OFW already uses -mregparm=1 for the callback address, but I don't know about powerpc.
I would prefer that we don't choose global CFLAGS to adjust one function. There is a "regparm" attribute that can be used on cmain() on i386 architecture.
But if you want to keep cmain() shared between architectures, then the cleanest approach would be to take care of grub_ieee1275_entry_fn before cmain() is called. It doesn't have to be in assembly. Sparc can still use its _start function to set grub_ieee1275_entry_fn and then call cmain().
Quoted 6 lines
Quoted 6 lines
kern/powerpc/ieee1275/crt0.S +0
-0@@ -38,5 +38,9 @@ _start: 2: stwu 2, 4(6) /* We know r2 is already 0 from above. */ bdnz 2b
Quoted 6 lines
kern/powerpc/ieee1275/crt0.S _start: · line 43
Quoted 3 lines
kern/powerpc/ieee1275/crt0.S +3
-0+ /* Store r5 in grub_ieee1275_entry_fn */ + lis 9, grub_ieee1275_entry_fn@ha + stw 5, grub_ieee1275_entry_fn@l(9)I thought you'd also need to remove stuff. Isn't there code above this that pushes the 3 parameters onto the stack?
Arguments are transmitted in registers r3, r4 and r5. Nothing is passed on the stack. r5 has the data we want to be in grub_ieee1275_entry_fn.
The first instruction takes the high 16 bits of the grub_ieee1275_entry_fn address and put is to register 9. Register 9 is the favorite choice for memory operations, as it has no special purpose and doesn't need to be preserved.
The second instruction places the contents of register 5 into an address pointed to by the register 9 plus the lower 16 bits of the grub_ieee1275_entry_fn address.
A 32-bit constant cannot be loaded in one operation, since all PowerPC commands are 32-bit, and it simply won't fit one command.
Signature · 2 lines
Regards, Pavel Roskin
Quoted 4 lines · Pavel Roskin
Oh, I didn't realize that
kern/powerpc/ieee1275/cmain.cis actually used by the i386-ieee1275 build. That's misleading! Perhaps it should be moved to kern/ieee1275/
Yes. I had this in mind, but I think it's better to wait untill the i386 port is complete before we start moving files.
Quoted 3 lines
Anyway, Sparc code doesn't use
cmain()in any way. It starts immediately with the function_start()written in C. So you shouldn't worry about breaking Sparc.
Ah, I see. I didn't expect this.
Quoted 2 lines
Please feel free to integrate the PowerPC part of my patch into your patch, as it indeed needs to be committed at once.
Ok. I'm attaching the complete patch.
Quoted 7 lines
Quoted 3 lines
Another option would be to have just one param. This would be the cleanest on i386, since the call from OFW already uses -mregparm=1 for the callback address, but I don't know about powerpc.
I would prefer that we don't choose global CFLAGS to adjust one function. There is a "regparm" attribute that can be used on
cmain()on i386 architecture.
The CFLAGS for i386-ieee1275 are already set to regparm. This makes it easier to interact with OFW in both ways, since call backs also use %eax as first (and only) parameter.
In fact, our current code seems to assume that grub_ieee1275_entry_fn() always has the same calling conventions than GRUB. Because of this, I thought it'd be consistent to assume the same for our _start routine.
However, powerpc and sparc64 seem to disagree with i386 on which is the parameter that ought to be used to initialize grub_ieee1275_entry_fn. I suppose it's better to move this out of cmain, then..
Quoted 5 lines
But if you want to keep
cmain()shared between architectures, then the cleanest approach would be to take care ofgrub_ieee1275_entry_fnbeforecmain()is called. It doesn't have to be in assembly. Sparc can still use its_startfunction to setgrub_ieee1275_entry_fnand then callcmain().
Ok. That sounds good.
Quoted 8 lines
Arguments are transmitted in registers r3, r4 and r5. Nothing is passed on the stack. r5 has the data we want to be in
grub_ieee1275_entry_fn.The first instruction takes the high 16 bits of the
grub_ieee1275_entry_fnaddress and put is to register 9. Register 9 is the favorite choice for memory operations, as it has no special purpose and doesn't need to be preserved.
Quoted 6 lines
The second instruction places the contents of register 5 into an address pointed to by the register 9 plus the lower 16 bits of the
grub_ieee1275_entry_fnaddress.A 32-bit constant cannot be loaded in one operation, since all PowerPC commands are 32-bit, and it simply won't fit one command.
I see..
Signature · 5 lines
Robert Millan <GPLv2> I know my rights; I want my phone call! <DRM> What use is a phone call… if you are unable to speak? (as seen on /.)
PowerPC changes provided by Pavel Roskin.
* kern/powerpc/ieee1275/cmain.c (cmain): Don't take any arguments.
* kern/powerpc/ieee1275/crt0.S: Store r5 in grub_ieee1275_entry_fn, don't rely on cmain() doing it.
* kern/i386/ieee1275/startup.S (_start): Store %eax in grub_ieee1275_entry_fn, don't rely on cmain() doing it.
kern/i386/ieee1275/startup.S +1 -1
@@ -34,5 +34,5 @@ start: _start: - movl %eax, %ecx + movl %eax, EXT_C(grub_ieee1275_entry_fn) jmp EXT_C(cmain)
kern/powerpc/ieee1275/cmain.c +3 -5
@@ -1,7 +1,7 @@/* cmain.c - Startup code for the PowerPC. */ /* * GRUB -- GRand Unified Bootloader -* Copyright (C) 2003,2004,2005,2006,2007 Free Software Foundation, Inc. +* Copyright (C) 2003,2004,2005,2006,2007,2008 Free Software Foundation, Inc. * * GRUB is free software: you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by
@@ -102,12 +102,10 @@ } } -void cmain (uint32_t r3,uint32_t r4,uint32_t r5); +void cmain (void );void -cmain (UNUSED uint32_t r3, UNUSED uint32_t r4, uint32_t r5) +cmain (void ) { - grub_ieee1275_entry_fn = (int (*)(void *)) r5; -grub_ieee1275_finddevice ("/chosen" , &grub_ieee1275_chosen);grub_ieee1275_find_options ();
kern/powerpc/ieee1275/crt0.S +5 -1
@@ -1,7 +1,7 @@ /* crt0.S - Startup code for the PowerPC. */ /* * GRUB -- GRand Unified Bootloader - * Copyright (C) 2003,2004,2005,2007 Free Software Foundation, Inc. + * Copyright (C) 2003,2004,2005,2007,2008 Free Software Foundation, Inc. * * GRUB is free software: you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by
@@ -38,5 +38,9 @@ 2: stwu 2, 4(6) /* We know r2 is already 0 from above. */ bdnz 2b + /* Store r5 in grub_ieee1275_entry_fn. */ + lis 9, grub_ieee1275_entry_fn@ha + stw 5, grub_ieee1275_entry_fn@l(9) + bl cmain 1: b 1b
Quoted 4 lines · Robert Millan
Quoted 2 lines
Please feel free to integrate the PowerPC part of my patch into your patch, as it indeed needs to be committed at once.
Ok. I'm attaching the complete patch.
Fine with me.
Signature · 2 lines
Regards, Pavel Roskin
Quoted 8 lines · Pavel Roskin
Quoted 4 lines · Robert Millan
Quoted 2 lines
Please feel free to integrate the PowerPC part of my patch into your patch, as it indeed needs to be committed at once.
Ok. I'm attaching the complete patch.
Fine with me.
Ok, checked in.
Signature · 5 lines
Robert Millan <GPLv2> I know my rights; I want my phone call! <DRM> What use is a phone call… if you are unable to speak? (as seen on /.)