At the risk of answering my own qusestion... Here's what I did to run the example kernel from Grub2, as a 64-bit image. Maybe somebofy will find this useful.
There's an example OS kernel that ships with the Grub Legacy tarball, in the docs directory. Grub2 will run that example okay if you compile it as an ELF32 image. I wanted to test Grub2 with an ELF64 image of the example kernel, and that required a few minor, but important changes. If you compile the example kernel on a "x86_64" platform, you'll get an ELF64 image with 64-bit executable code. The problem is, when Grub jumps to the entry point of the OS, the machine is in 32-bit mode and cannot execute code that was compiled for 64-bit. The key is to create 32-bit entry point code and stuff it into an ELF64 image. The example kernel comes in three files: boot.S, kernel.c and multiboot.h. The following changes are required: The assembler directive ".code32" needs to be added to boot.S.You also need to compile kernel.c to generate the assemler version kernel.s. Then you need to add the ".code32" directive to kernel.sCompile the modified boot.S and kernel.s to create object files.Finally, compile the object files to create an ELF64 image. If you want to automate this stuff with a script, start by creating a file called "dot_code32" that contains two lines, the first of which is the string ".code32" The second line is blank. Then do the following in a script: cat dot_code32 boot.S > boot32.S gcc -c boot32.S gcc -S -m32 -nostdlib kernel.c cat dot_code32 kernel.s > kernel32.s gcc -c kernel32.s gcc -o kernel64 -nostdlib kernel32.o boot32.o Now you got an ELF64 executable called "kernel64" that runs 32-bit code at the entry point. Grub2 will bootload this one without a problem. Dov Bulka --- On Wed, 11/11/09, [email protected] <[email protected]> wrote: From: [email protected] <[email protected]> Subject: Help-grub Digest, Vol 22, Issue 5 To: [email protected] Date: Wednesday, November 11, 2009, 12:03 PM Send Help-grub mailing list submissions to [email protected] To subscribe or unsubscribe via the World Wide Web, visit http://lists.gnu.org/mailman/listinfo/help-grub or, via email, send a message with subject or body 'help' to [email protected] You can reach the person managing the list at [email protected] When replying, please edit your Subject line so it is more specific than "Re: Contents of Help-grub digest..." Today's Topics: 1. multiboot kernel example in Grub2 tarball? (Dr. Dov Bulka) ---------------------------------------------------------------------- Message: 1 Date: Tue, 10 Nov 2009 18:22:54 -0800 (PST) From: "Dr. Dov Bulka" <[email protected]> Subject: multiboot kernel example in Grub2 tarball? To: [email protected] Message-ID: <[email protected]> Content-Type: text/plain; charset="iso-8859-1" The Grub Legacy tarball included an example multiboot kernel. The sources were in the "docs" directory (boot.S, multiboot.h and kernel.c) This was a 32-bit example. Does the Grub2 tarball contain such an example kernel? The example source code from Grub Legacy does not compile on an "x86_64" platform since the assembly language syntax for "x86_64" is different than "i386". Does anyone have a link to such an "x86_64" multiboot kernel example? TIA, Dov -------------- next part -------------- An HTML attachment was scrubbed... URL: http://lists.gnu.org/pipermail/help-grub/attachments/20091110/20cc60d1/attachment.html ------------------------------ _______________________________________________ Help-grub mailing list [email protected] http://lists.gnu.org/mailman/listinfo/help-grub End of Help-grub Digest, Vol 22, Issue 5 ****************************************
_______________________________________________ Help-grub mailing list [email protected] http://lists.gnu.org/mailman/listinfo/help-grub
