All of lore.kernel.org
 help / color / mirror / Atom feed
From: Robert Millan <rmh@aybabtu.com>
To: grub-devel@gnu.org
Subject: [PATCH] swap real_to_prot() and prot_to_real() (Re: [PATCH] i386-qemu port)
Date: Sun, 21 Jun 2009 21:52:37 +0200	[thread overview]
Message-ID: <20090621195237.GA23624@thorin> (raw)
In-Reply-To: <20090621181748.GA21152@thorin>

[-- Attachment #1: Type: text/plain, Size: 828 bytes --]


When doing the i386-coreboot port I made this choice completely backwards.

I thought real_to_prot() was only useful on i386-pc, because we needed it
for returning from BIOS, and prot_to_real() was useful elsewhere, because
the Linux loader would use it.

Turns out we need real_to_prot() on i386-qemu for the initial transition
to i386 mode, AND we don't need prot_to_real() anywhere other than i386-pc,
because OSes that expect to be loaded in i8086 mode are going to rely on
BIOS calls.

So this patch swaps them.  real_to_prot() goes to realmode.S and
prot_to_real() back to startup.S.

-- 
Robert Millan

  The DRM opt-in fallacy: "Your data belongs to us. We will decide when (and
  how) you may access your data; but nobody's threatening your freedom: we
  still allow you to remove your data and not access it at all."

[-- Attachment #2: real_prot.diff --]
[-- Type: text/x-diff, Size: 5550 bytes --]

2009-06-21  Robert Millan  <rmh.grub@aybabtu.com>

	* kern/i386/pc/startup.S (real_to_prot): Move from here ...
	* kern/i386/realmode.S (real_to_prot): ... to here.

	* kern/i386/realmode.S (prot_to_real): Move from here ...
	* kern/i386/pc/startup.S (prot_to_real): ... to here.

Index: kern/i386/pc/startup.S
===================================================================
--- kern/i386/pc/startup.S	(revision 2353)
+++ kern/i386/pc/startup.S	(working copy)
@@ -302,62 +302,73 @@
 	.p2align	2	/* force 4-byte alignment */
 
 /*
- *  These next two routines, "real_to_prot" and "prot_to_real" are structured
- *  in a very specific way.  Be very careful when changing them.
+ *  This next routine, "prot_to_real" is structured in a very
+ *  specific way.  Be very careful when changing it.
  *
- *  NOTE:  Use of either one messes up %eax and %ebp.
+ *  NOTE:  Use of it messes up %eax and %ebp.
  */
 
-real_to_prot:
-	.code16
-	cli
+prot_to_real:
+	/* just in case, set GDT */
+	lgdt	gdtdesc
 
-	/* load the GDT register */
-#ifdef APPLE_CC
-	mov %cs, %ax
-	mov %ax, %ds
-	DATA32	ADDR32	lgdt	gdtdesc
-#else
-	DATA32	ADDR32	lgdt	%cs:gdtdesc
-#endif
+	/* save the protected mode stack */
+	movl	%esp, %eax
+	movl	%eax, protstack
 
-	/* turn on protected mode */
-	movl	%cr0, %eax
-	orl	$GRUB_MEMORY_MACHINE_CR0_PE_ON, %eax
-	movl	%eax, %cr0
+	/* get the return address */
+	movl	(%esp), %eax
+	movl	%eax, GRUB_MEMORY_MACHINE_REAL_STACK
 
-	/* jump to relocation, flush prefetch queue, and reload %cs */
-	DATA32	ljmp	$GRUB_MEMORY_MACHINE_PROT_MODE_CSEG, $protcseg
+	/* set up new stack */
+	movl	$GRUB_MEMORY_MACHINE_REAL_STACK, %eax
+	movl	%eax, %esp
+	movl	%eax, %ebp
 
-	.code32
-protcseg:
-	/* reload other segment registers */
-	movw	$GRUB_MEMORY_MACHINE_PROT_MODE_DSEG, %ax
+	/* set up segment limits */
+	movw	$GRUB_MEMORY_MACHINE_PSEUDO_REAL_DSEG, %ax
 	movw	%ax, %ds
 	movw	%ax, %es
 	movw	%ax, %fs
 	movw	%ax, %gs
 	movw	%ax, %ss
 
-	/* put the return address in a known safe location */
-	movl	(%esp), %eax
-	movl	%eax, GRUB_MEMORY_MACHINE_REAL_STACK
+	/* this might be an extra step */
+	/* jump to a 16 bit segment */
+	ljmp	$GRUB_MEMORY_MACHINE_PSEUDO_REAL_CSEG, $tmpcseg
 
-	/* get protected mode stack */
-	movl	protstack, %eax
-	movl	%eax, %esp
-	movl	%eax, %ebp
+tmpcseg:
+	.code16
 
-	/* get return address onto the right stack */
-	movl	GRUB_MEMORY_MACHINE_REAL_STACK, %eax
-	movl	%eax, (%esp)
+	/* clear the PE bit of CR0 */
+	movl	%cr0, %eax
+	andl 	$(~GRUB_MEMORY_MACHINE_CR0_PE_ON), %eax
+	movl	%eax, %cr0
 
+	/* flush prefetch queue, reload %cs */
+	DATA32	ljmp	$0, $realcseg
+
+realcseg:
+	/* we are in real mode now
+	 * set up the real mode segment registers : DS, SS, ES
+	 */
 	/* zero %eax */
 	xorl	%eax, %eax
 
-	/* return on the old (or initialized) stack! */
-	ret
+	movw	%ax, %ds
+	movw	%ax, %es
+	movw	%ax, %fs
+	movw	%ax, %gs
+	movw	%ax, %ss
 
+	/* restore interrupts */
+	sti
+
+	/* return on new stack! */
+	DATA32	ret
+
+	.code32
+
 /*
  * grub_gate_a20(int on)
  *
Index: kern/i386/realmode.S
===================================================================
--- kern/i386/realmode.S	(revision 2353)
+++ kern/i386/realmode.S	(working copy)
@@ -110,69 +110,59 @@
 	.long	gdt			/* addr */
 
 /*
- *  These next routine, "prot_to_real" is structured in a very
+ *  This next routine, "real_to_prot" is structured in a very
  *  specific way.  Be very careful when changing it.
  *
  *  NOTE:  Use of it messes up %eax and %ebp.
  */
 
-prot_to_real:
-	/* just in case, set GDT */
-	lgdt	gdtdesc
+real_to_prot:
+	.code16
+	cli
 
-	/* save the protected mode stack */
-	movl	%esp, %eax
-	movl	%eax, protstack
+	/* load the GDT register */
+#ifdef APPLE_CC
+	mov %cs, %ax
+	mov %ax, %ds
+	DATA32	ADDR32	lgdt	gdtdesc
+#else
+	DATA32	ADDR32	lgdt	%cs:gdtdesc
+#endif
 
-	/* get the return address */
-	movl	(%esp), %eax
-	movl	%eax, GRUB_MEMORY_MACHINE_REAL_STACK
+	/* turn on protected mode */
+	movl	%cr0, %eax
+	orl	$GRUB_MEMORY_MACHINE_CR0_PE_ON, %eax
+	movl	%eax, %cr0
 
-	/* set up new stack */
-	movl	$GRUB_MEMORY_MACHINE_REAL_STACK, %eax
-	movl	%eax, %esp
-	movl	%eax, %ebp
+	/* jump to relocation, flush prefetch queue, and reload %cs */
+	DATA32	ljmp	$GRUB_MEMORY_MACHINE_PROT_MODE_CSEG, $protcseg
 
-	/* set up segment limits */
-	movw	$GRUB_MEMORY_MACHINE_PSEUDO_REAL_DSEG, %ax
+	.code32
+protcseg:
+	/* reload other segment registers */
+	movw	$GRUB_MEMORY_MACHINE_PROT_MODE_DSEG, %ax
 	movw	%ax, %ds
 	movw	%ax, %es
 	movw	%ax, %fs
 	movw	%ax, %gs
 	movw	%ax, %ss
 
-	/* this might be an extra step */
-	/* jump to a 16 bit segment */
-	ljmp	$GRUB_MEMORY_MACHINE_PSEUDO_REAL_CSEG, $tmpcseg
+	/* put the return address in a known safe location */
+	movl	(%esp), %eax
+	movl	%eax, GRUB_MEMORY_MACHINE_REAL_STACK
 
-tmpcseg:
-	.code16
+	/* get protected mode stack */
+	movl	protstack, %eax
+	movl	%eax, %esp
+	movl	%eax, %ebp
 
-	/* clear the PE bit of CR0 */
-	movl	%cr0, %eax
-	andl 	$(~GRUB_MEMORY_MACHINE_CR0_PE_ON), %eax
-	movl	%eax, %cr0
+	/* get return address onto the right stack */
+	movl	GRUB_MEMORY_MACHINE_REAL_STACK, %eax
+	movl	%eax, (%esp)
 
-	/* flush prefetch queue, reload %cs */
-	DATA32	ljmp	$0, $realcseg
-
-realcseg:
-	/* we are in real mode now
-	 * set up the real mode segment registers : DS, SS, ES
-	 */
 	/* zero %eax */
 	xorl	%eax, %eax
 
-	movw	%ax, %ds
-	movw	%ax, %es
-	movw	%ax, %fs
-	movw	%ax, %gs
-	movw	%ax, %ss
-
-	/* restore interrupts */
-	sti
-
-	/* return on new stack! */
-	DATA32	ret
-
+	/* return on the old (or initialized) stack! */
+	ret
 	.code32

  parent reply	other threads:[~2009-06-21 19:52 UTC|newest]

Thread overview: 71+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2009-06-21 18:17 [PATCH] i386-qemu port Robert Millan
2009-06-21 18:50 ` does module area require alignment? (Re: [PATCH] i386-qemu port) Robert Millan
2009-06-21 19:08   ` Pavel Roskin
2009-06-21 19:33     ` Robert Millan
2009-06-22 12:31       ` [PATCH] define GRUB_MOD_ALIGN to 0 on non-ieee1275 (Re: does module area require alignment? (Re: [PATCH] i386-qemu port)) Robert Millan
2009-06-22 19:43         ` Pavel Roskin
2009-06-22 20:41           ` Robert Millan
2009-06-22 20:51             ` Pavel Roskin
2009-06-22 21:22               ` Robert Millan
2009-06-22 21:45                 ` Pavel Roskin
2009-06-22 22:31                   ` Robert Millan
2009-06-22 19:51       ` does module area require alignment? (Re: [PATCH] i386-qemu port) Pavel Roskin
2009-06-22 22:50         ` Vladimir 'phcoder' Serbinenko
2009-06-23  0:10           ` Pavel Roskin
2009-06-21 18:54 ` [PATCH] move grub_stop() " Robert Millan
2009-06-21 19:05   ` Pavel Roskin
2009-06-21 19:25     ` Robert Millan
2009-06-22  2:14       ` Pavel Roskin
2009-06-22 10:10         ` Robert Millan
2009-06-22 16:16           ` Pavel Roskin
2009-06-22 18:05             ` Robert Millan
2009-06-21 19:00 ` [PATCH] i386-qemu port Pavel Roskin
2009-06-21 19:30   ` Robert Millan
2009-06-22 12:45     ` Robert Millan
2009-06-21 20:34   ` Robert Millan
2009-06-21 20:40     ` Vladimir 'phcoder' Serbinenko
2009-06-21 19:19 ` [PATCH] rename kernel.elf to kernel.img (Re: [PATCH] i386-qemu port) Robert Millan
2009-06-22  2:20   ` Pavel Roskin
2009-06-22 10:27     ` Robert Millan
2009-06-21 19:52 ` Robert Millan [this message]
2009-06-22  1:56   ` [PATCH] swap real_to_prot() and prot_to_real() " Pavel Roskin
2009-06-22 10:45     ` Robert Millan
2009-06-21 20:22 ` [PATCH] i386-qemu port Robert Millan
2009-06-22  1:50   ` Pavel Roskin
2009-06-22 10:57     ` Robert Millan
2009-06-21 22:53 ` [PATCH] access gdtdesc on segment 0 unconditionally (Re: [PATCH] i386-qemu port) Robert Millan
2009-06-22  1:22   ` Pavel Roskin
2009-06-22  9:52     ` Robert Millan
2009-06-22 19:39       ` Pavel Roskin
2009-06-22 20:52         ` Robert Millan
2009-06-22 21:32           ` Robert Millan
2009-06-22 21:44             ` Pavel Roskin
2009-06-22 22:43               ` Robert Millan
2009-06-23  0:53                 ` Pavel Roskin
2009-06-23 11:02                   ` Robert Millan
2009-06-22 21:36           ` Pavel Roskin
2009-06-22 22:52             ` Robert Millan
2009-06-22 10:26     ` about Apple compiler (Re: [PATCH] access gdtdesc on segment 0 unconditionally (Re: [PATCH] i386-qemu port)) Robert Millan
2009-06-22 16:10       ` Pavel Roskin
2009-06-22 15:02 ` [PATCH] s/GRUB_MEMORY_MACHINE_LINK_ADDR/GRUB_KERNEL_MACHINE_LINK_ADDR/g (Re: [PATCH] i386-qemu port) Robert Millan
2009-06-22 19:00   ` Pavel Roskin
2009-06-22 23:07 ` clean patch for i386-qemu port " Robert Millan
2009-06-23  1:29   ` Pavel Roskin
2009-06-23 11:38     ` Robert Millan
2009-06-23 12:13       ` Robert Millan
2009-06-24  1:00         ` Robert Millan
2009-06-24 23:10           ` [PATCH] fix for loading modules from read-only memory area (Re: clean patch for i386-qemu port (Re: [PATCH] i386-qemu port)) Robert Millan
2009-06-25 19:53             ` Pavel Roskin
2009-06-25 20:31               ` Robert Millan
2009-06-25 20:51                 ` Pavel Roskin
2009-06-26 14:41                   ` Robert Millan
2009-06-26 16:44                     ` Pavel Roskin
2009-06-26 17:03                       ` Robert Millan
2009-06-26 17:16                         ` Pavel Roskin
2009-06-26 17:43                           ` Robert Millan
2009-06-26 19:52                             ` Pavel Roskin
2009-06-26 22:26                             ` Pavel Roskin
2009-06-26 23:57                               ` Robert Millan
2009-06-27  3:08                                 ` Pavel Roskin
2009-06-27 11:18                                   ` Robert Millan
2009-06-29  3:48                                     ` Pavel Roskin

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20090621195237.GA23624@thorin \
    --to=rmh@aybabtu.com \
    --cc=grub-devel@gnu.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.