Skip to contentSkip to conversation

Relocator framework

PATCH In review Vladimir 'phcoder' Serbinenko 4 participants 9 messages Aug 3, 2009 – Aug 8, 2009

lore
Share & tools
lore
#0 lore raw

PATCH 1/2 Relocator framework

Hello. As discussed on IRC it would be preferable if all loaders use flexible technics for loading kernels using relocators as currently multiboot and xnu does. Here is a relocator framework based on multiboot and xnu relocator. As an advantage it switches from x86-64 to i386 if necessary. With it and some more patches I was able to load mbtest on qemu64 with tianocore

Signature · 4 lines
Regards
Vladimir 'phcoder' Serbinenko

Personal git repository: http://repo.or.cz/w/grub2/phcoder.git
conf/i386.rmk +6 -0
index 93f84ce..33d49b5 100644
@@ -14,3 +14,9 @@ pkglib_MODULES += vga_text.mod
 vga_text_mod_SOURCES = term/i386/pc/vga_text.c term/i386/vga_common.c
 vga_text_mod_CFLAGS = $(COMMON_CFLAGS)
 vga_text_mod_LDFLAGS = $(COMMON_LDFLAGS)
+
+pkglib_MODULES += relocator.mod
+relocator_mod_SOURCES = lib/i386/relocator.c lib/i386/relocator_asm.S lib/i386/relocator_backward.S
+relocator_mod_CFLAGS = $(COMMON_CFLAGS)
+relocator_mod_ASFLAGS = $(COMMON_ASFLAGS)
+relocator_mod_LDFLAGS = $(COMMON_LDFLAGS)
include/grub/i386/relocator.h new file +40 -0
new file mode 100644
index 0000000..cbc75ed
@@ -0,0 +1,40 @@
+/*
+ *  GRUB  --  GRand Unified Bootloader
+ *  Copyright (C) 2009  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
+ *  the Free Software Foundation, either version 3 of the License, or
+ *  (at your option) any later version.
+ *
+ *  GRUB is distributed in the hope that it will be useful,
+ *  but WITHOUT ANY WARRANTY; without even the implied warranty of
+ *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ *  GNU General Public License for more details.
+ *
+ *  You should have received a copy of the GNU General Public License
+ *  along with GRUB.  If not, see <http://www.gnu.org/licenses/>.
+ */
+
+#ifndef GRUB_RELOCATOR_CPU_HEADER
+#define GRUB_RELOCATOR_CPU_HEADER	1
+
+struct grub_relocator32_state
+{
+  grub_uint32_t esp;
+  grub_uint32_t eax;
+  grub_uint32_t ebx;
+  grub_uint32_t ecx;
+  grub_uint32_t edx;
+  grub_uint32_t eip;
+};
+
+void *
+grub_relocator32_alloc (grub_size_t size);
+grub_err_t
+grub_relocator32_boot (void *relocator, grub_uint32_t dest,
+		       struct grub_relocator32_state state);
+void
+grub_relocator32_free (void *relocator);
+
+#endif /* ! GRUB_RELOCATOR_CPU_HEADER */
lib/i386/relocator.c new file +154 -0

154 lines not on this page · raw message

lib/i386/relocator_asm.S new file +273 -0

273 lines not on this page · raw message

lib/i386/relocator_backward.S new file +2 -0
new file mode 100644
index 0000000..0691347
@@ -0,0 +1,2 @@
+#define BACKWARD
+#include "relocator_asm.S"
#1 lore raw
Quoted 6 lines · Vladimir 'phcoder' Serbinenko

Hello. As discussed on IRC it would be preferable if all loaders use flexible technics for loading kernels using relocators as currently multiboot and xnu does. Here is a relocator framework based on multiboot and xnu relocator. As an advantage it switches from x86-64 to i386 if necessary. With it and some more patches I was able to load mbtest on qemu64 with tianocore

Small update: new function for increasing relocatable space
Can someone test it with situations requiring backward allocator? (dest > src) A problem which is perhaps currently present is if abs(desc-src) < RELOCATOR_SIZE then relocator may overwrite itself and fail. A possible solution is to allocate a bit more space in relocatable buffer to be able to change src by at most RELOCATOR_SIZE - this will be enough to set dest=src in these situations and use a "null" relocator (to be implemented)

Quoted 7 lines

--
Regards
Vladimir 'phcoder' Serbinenko

Personal git repository: http://repo.or.cz/w/grub2/phcoder.git

Signature · 4 lines
Regards
Vladimir 'phcoder' Serbinenko

Personal git repository: http://repo.or.cz/w/grub2/phcoder.git
conf/i386.rmk +6 -0
index 93f84ce..33d49b5 100644
@@ -14,3 +14,9 @@ pkglib_MODULES += vga_text.mod
 vga_text_mod_SOURCES = term/i386/pc/vga_text.c term/i386/vga_common.c
 vga_text_mod_CFLAGS = $(COMMON_CFLAGS)
 vga_text_mod_LDFLAGS = $(COMMON_LDFLAGS)
+
+pkglib_MODULES += relocator.mod
+relocator_mod_SOURCES = lib/i386/relocator.c lib/i386/relocator_asm.S lib/i386/relocator_backward.S
+relocator_mod_CFLAGS = $(COMMON_CFLAGS)
+relocator_mod_ASFLAGS = $(COMMON_ASFLAGS)
+relocator_mod_LDFLAGS = $(COMMON_LDFLAGS)
include/grub/i386/relocator.h new file +41 -0

41 lines not on this page · raw message

lib/i386/relocator.c new file +173 -0

173 lines not on this page · raw message

lib/i386/relocator_asm.S new file +273 -0

273 lines not on this page · raw message

lib/i386/relocator_backward.S new file +2 -0
new file mode 100644
index 0000000..0691347
@@ -0,0 +1,2 @@
+#define BACKWARD
+#include "relocator_asm.S"
#2 lore raw
In reply to Vladimir 'phcoder' Serbinenko
Quoted 3 lines · Vladimir 'phcoder' Serbinenko

Hello. As discussed on IRC it would be preferable if all loaders use flexible technics for loading kernels using relocators as currently multiboot and xnu does.

Well, as for what I said on IRC, I'm not sure if it'd be preferable. I think it *could* be, if it makes things simpler & more maintainable (which greatly depends on how the patches for multiboot.c/linux.c look like).

Anyway, I'll comment some things on this patch:

lib/i386/relocator.c line 39

Quoted 5 lines
lib/i386/relocator.c new file +5 -0
+#ifdef __x86_64__
+extern grub_uint64_t grub_relocator32_backward_src;
+#else
+extern grub_uint32_t grub_relocator32_backward_src;
+#endif

You could make this a pointer, or grub_uintptr_t (the latter we don't yet have, it seems like a good excuse to add it if a pointer is not suitable :-)).

lib/i386/relocator_asm.S line 39

Quoted 6 lines
lib/i386/relocator_asm.S new file +6 -0
+#ifndef __x86_64__
+	/* mov imm32, %eax */
+	.byte	0xb8
+RELOCATOR_VARIABLE(dest)
+	.long 0
+	mov %eax, %edi

Please use size qualifiers (e.g. movl).

Also, remember to indent the first parameter like we do elsewhere (e.g. ".long\t0", "movl\t%eax, %edi", etc).

lib/i386/relocator_asm.S line 69

Quoted 11 lines
lib/i386/relocator_asm.S new file +11 -0
+	/* Backward movsl is implicitly off-by-four.  compensate that.  */
+	subl	$4,	%esi
+	subl	$4,	%edi
+
+	/* Backward copy.  */
+	std
+	addl	%ecx, %esi
+	addl	%ecx, %edi
+
+	rep
+	movsl

Are you sure moving to movsl is a good idea? Maybe the payload size is not 4-aligned.

lib/i386/relocator_asm.S line 137

Quoted 5 lines
lib/i386/relocator_asm.S new file +5 -0
+#ifdef APPLE_CC
+	add $(cont0 - base), %eax
+#else
+	add $(cont0 - base), %rax
+#endif

What's the issue at hand? Apple assembler [1] can't add an inmediate to %rax ?

Truncating it seems like it could be problematic if %eax overflows.

[1] btw, APPLE_CC macro for assembler checks is really confusing

lib/i386/relocator_asm.S line 152

Quoted 2 lines
lib/i386/relocator_asm.S new file +2 -0
+	/* Update %cs. Thanks to David Miller for pointing this mistake out. */
+	ljmp *(jump_vector - base) (%esi,1)

Comments ought to be for useful information. For praise/acknoledgements (and I think David deserves it) we have the THANKS file.

Quoted 5 lines
lib/i386/relocator_asm.S new file +5 -0
+	/* Disable paging. */
+	mov %cr0, %eax
+	and $0x7fffffff, %eax
+	mov %eax, %cr0
+

lib/i386/relocator_asm.S line 193

Quoted 10 lines
lib/i386/relocator_asm.S new file +10 -0
+	/* Disable amd64. */
+	mov $0xc0000080, %ecx
+	rdmsr
+	and $0xfffffeff, %eax
+	wrmsr
+
+	/* Turn off PAE. */
+	movl %cr4, %eax
+	and $0xffffffcf, %eax
+	mov %eax, %cr4

Please use macros for such things (see GRUB_MEMORY_MACHINE_CR0_PE_ON).

Signature · 5 lines
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."
#3 lore raw
Quoted 9 lines

lib/i386/relocator.c line 39

Quoted 5 lines
lib/i386/relocator.c new file +5 -0
+#ifdef __x86_64__
+extern grub_uint64_t grub_relocator32_backward_src;
+#else
+extern grub_uint32_t grub_relocator32_backward_src;
+#endif

You could make this a pointer, or grub_uintptr_t (the latter we don't yet have, it seems like a good excuse to add it if a pointer is not suitable :-)).

grub_addr_t would actually do the job.

Quoted 10 lines

lib/i386/relocator_asm.S line 39

Quoted 7 lines
lib/i386/relocator_asm.S new file +6 -0
+#ifndef __x86_64__
+     /* mov imm32, %eax */
+     .byte   0xb8
+RELOCATOR_VARIABLE(dest)
+     .long 0
+     mov %eax, %edi

Please use size qualifiers (e.g. movl).

Quoted 3 lines

Also, remember to indent the first parameter like we do elsewhere (e.g. ".long\t0", "movl\t%eax, %edi", etc).

Ok

Quoted 6 lines
Quoted 6 lines

+     /* Backward movsl is implicitly off-by-four.  compensate that.  */
+     subl    $4,     %esi
+     subl    $4,     %edi
+
+     /* Backward copy.  */
+     std

Quoted 9 lines
Quoted 5 lines

+     addl    %ecx, %esi
+     addl    %ecx, %edi
+
+     rep
+     movsl

Are you sure moving to movsl is a good idea?  Maybe the payload size is not 4-aligned.

On AFAIK x86 movsl works on unaligned addresses too. I'll recheck

Quoted 9 lines

lib/i386/relocator_asm.S line 137

Quoted 5 lines
lib/i386/relocator_asm.S new file +5 -0
+#ifdef APPLE_CC
+     add $(cont0 - base), %eax
+#else
+     add $(cont0 - base), %rax
+#endif

What's the issue at hand?  Apple assembler [1] can't add an inmediate to %rax ?

Apple linker can't handle 64-bit differences

Quoted 2 lines

Truncating it seems like it could be problematic if %eax overflows.

Actually it's not a problem since this code is put together with kernel which executes in 32-bit mode so it can't be over 4GiB. If we base 64-bit relocator it will be a problem if OS wants to go over 4GiB.

Quoted 2 lines

[1] btw, APPLE_CC macro for assembler checks is really confusing

I'll try with __apple__

Quoted 1 line

Please use macros for such things (see GRUB_MEMORY_MACHINE_CR0_PE_ON).

Will look into it

Quoted 14 lines

--
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."

_______________________________________________
Grub-devel mailing list
[email protected]
http://lists.gnu.org/mailman/listinfo/grub-devel

Signature · 4 lines
Regards
Vladimir 'phcoder' Serbinenko

Personal git repository: http://repo.or.cz/w/grub2/phcoder.git
#4 lore raw
Quoted 10 lines · Vladimir 'phcoder' Serbinenko
Quoted 9 lines

lib/i386/relocator.c line 39

Quoted 5 lines
lib/i386/relocator.c new file +5 -0
+#ifdef __x86_64__
+extern grub_uint64_t grub_relocator32_backward_src;
+#else
+extern grub_uint32_t grub_relocator32_backward_src;
+#endif

You could make this a pointer, or grub_uintptr_t (the latter we don't yet have, it seems like a good excuse to add it if a pointer is not suitable :-)).

grub_addr_t would actually do the job.

Ah yes, of course.

Quoted 4 lines
Quoted 3 lines

Are you sure moving to movsl is a good idea?  Maybe the payload size is not 4-aligned.

On AFAIK x86 movsl works on unaligned addresses too. I'll recheck

But maybe there's some corner case in which we would overwrite something if we copy 3 bytes more than necessary? I didn't check if this would be possible.

Quoted 10 lines
Quoted 9 lines

lib/i386/relocator_asm.S line 137

Quoted 5 lines
lib/i386/relocator_asm.S new file +5 -0
+#ifdef APPLE_CC
+     add $(cont0 - base), %eax
+#else
+     add $(cont0 - base), %rax
+#endif

What's the issue at hand?  Apple assembler [1] can't add an inmediate to %rax ?

Apple linker can't handle 64-bit differences

This seems like a bug. Can GNU binutils be used on MacOS to resolve this?

If it's workable, I'd rather make binutils a build requirement than adding more of this (the other APPLE_CC ifdefs will probably need some review too, but there's no hurry about that).

Signature · 5 lines
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."
#5 lore raw

Robert Millan <[email protected]> writes:

[...]

Quoted 7 lines
Quoted 1 line

Apple linker can't handle 64-bit differences

This seems like a bug. Can GNU binutils be used on MacOS to resolve this?

If it's workable, I'd rather make binutils a build requirement than adding more of this (the other APPLE_CC ifdefs will probably need some review too, but there's no hurry about that).

Agreed. GRUB is a GNU project and we can depend on GNU programs. Especially since we use some GNU extensions.

--
Marco

#6 lore raw
Quoted 6 lines · Marco Gerards

Robert Millan <[email protected]> writes:

[...]

Quoted 2 lines
Quoted 1 line

Apple linker can't handle 64-bit differences

Quoted 9 lines
Quoted 5 lines

This seems like a bug.  Can GNU binutils be used on MacOS to resolve this?

If it's workable, I'd rather make binutils a build requirement than adding more of this (the other APPLE_CC ifdefs will probably need some review too, but there's no hurry about that).

Agreed.  GRUB is a GNU project and we can depend on GNU programs. Especially since we use some GNU extensions.

Apple's compiler is based GCC but binutils aren't and they pose the most of problems. Actualy the most problematic bit was that I didn't know that unless you prefix variable with L_ apple's assembler treats it as global. Adding L_ in this code solves problem. Yves proposed to add a LOCAL(x) macro to symbol.h this way the assembler code is also more readable. I also did further adjustments in this patch to decrease the number of ifdefs but had no time to test it and so couldn't submit it

Quoted 10 lines

--
Marco

_______________________________________________
Grub-devel mailing list
[email protected]
http://lists.gnu.org/mailman/listinfo/grub-devel

Signature · 4 lines
Regards
Vladimir 'phcoder' Serbinenko

Personal git repository: http://repo.or.cz/w/grub2/phcoder.git
#7 lore raw
Quoted 8 lines · Vladimir 'phcoder' Serbinenko

Apple's compiler is based GCC but binutils aren't and they pose the most of problems. Actualy the most problematic bit was that I didn't know that unless you prefix variable with L_ apple's assembler treats it as global. Adding L_ in this code solves problem. Yves proposed to add a LOCAL(x) macro to symbol.h this way the assembler code is also more readable. I also did further adjustments in this patch to decrease the number of ifdefs but had no time to test it and so couldn't submit it

I would prefer if you don't commit any patches that introduce any more preprocessor conditionals for the Apple compiler or assembler.

It would make it hard to debug problems reported by others, as different compilers would produce different binaries.

If we are using some feature that is just nice but not needed (such as binary & in lnxboot.S to set the image size to 1024), it would be better to avoid using that feature for all compilers rather than for the compilers that don't support it.

Signature · 2 lines
Regards,
Pavel Roskin
#8 lore raw
Quoted 11 lines · Pavel Roskin
Quoted 8 lines · Vladimir 'phcoder' Serbinenko

Apple's compiler is based GCC but binutils aren't and they pose the most of problems. Actualy the most problematic bit was that I didn't know that unless you prefix variable with L_ apple's assembler treats it as global. Adding L_ in this code solves problem. Yves proposed to add a LOCAL(x) macro to symbol.h this way the assembler code is also more readable. I also did further adjustments in this patch to decrease the number of ifdefs but had no time to test it and so couldn't submit it

Quoted 2 lines

I would prefer if you don't commit any patches that introduce any more preprocessor conditionals for the Apple compiler or assembler.

LOCAL(x) can always be expanded to L_## x but it will make code more readable and modifiable in future. And it will allow a bunch of other ifdef's go out.

Quoted 4 lines

It would make it hard to debug problems reported by others, as different compilers would produce different binaries.

Even just changing compiler options you produce different binary

Quoted 5 lines

If we are using some feature that is just nice but not needed (such as binary & in lnxboot.S to set the image size to 1024), it would be better to avoid using that feature for all compilers rather than for the compilers that don't support it.

lnxboot.S with Apple's toolchain is disabled temporarily awaiting someone to find a definitive solution.
Why do our 2 new maintainers propose to shave off useful features?

Quoted 10 lines

--
Regards,
Pavel Roskin

_______________________________________________
Grub-devel mailing list
[email protected]
http://lists.gnu.org/mailman/listinfo/grub-devel

Signature · 4 lines
Regards
Vladimir 'phcoder' Serbinenko

Personal git repository: http://repo.or.cz/w/grub2/phcoder.git