Skip to contentSkip to conversation

x86 boot: simplify reloc.c

PATCH In review Xiao Guangrong 2 participants 3 messages Nov 10, 2009

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 x86 boot: simplify reloc.c

This patch simplify reloc.c with:
1): no need separate make
reloc.c -> reloc.o -> reloc.lnk -> reloc.bin -> reloc.S, and static embed reloc.S into head.S
2): reuse memcpy() in string lib
3): remove assemble code in the head of reloc.c

[ It is just a cleanup patch and not change the code's logic, and it work well on x86.
Not have x86_64 machine in my hand, thanks very much if someone can test it on x86_64 ]

xen/arch/x86/boot/build32.mk +0 -25
diff -Nur a/xen/arch/x86/boot/build32.mk b/xen/arch/x86/boot/build32.mk
@@ -1,25 +0,0 @@
-XEN_ROOT=../../../..
-override XEN_TARGET_ARCH=x86_32
-CFLAGS =
-include $(XEN_ROOT)/Config.mk
-
-# Disable PIE/SSP if GCC supports them. They can break us.
-$(call cc-option-add,CFLAGS,CC,-nopie)
-$(call cc-option-add,CFLAGS,CC,-fno-stack-protector)
-$(call cc-option-add,CFLAGS,CC,-fno-stack-protector-all)
-
-CFLAGS += -Werror -fno-builtin -msoft-float
-
-# NB. awk invocation is a portable alternative to 'head -n -1'
-%.S: %.bin
-	(od -v -t x $< | awk 'NR > 1 {print s} {s=$$0}' | \
-	sed 's/ /,0x/g' | sed 's/^[0-9]*,/ .long /') >$@
-
-%.bin: %.lnk
-	$(OBJCOPY) -O binary $< $@
-
-%.lnk: %.o
-	$(LD) $(LDFLAGS_DIRECT) -N -Ttext 0x8c000 -o $@ $<
-
-%.o: %.c
-	$(CC) $(CFLAGS) -c $< -o $@
xen/arch/x86/boot/head.S +0 -3
diff -Nur a/xen/arch/x86/boot/head.S b/xen/arch/x86/boot/head.S
@@ -195,9 +195,6 @@
 
 #include "cmdline.S"
 
-reloc:
-#include "reloc.S"
-
         .align 16
         .globl trampoline_start, trampoline_end
 trampoline_start:
xen/arch/x86/boot/Makefile +1 -6
diff -Nur a/xen/arch/x86/boot/Makefile b/xen/arch/x86/boot/Makefile
@@ -1,7 +1,2 @@
 obj-y += head.o
-
-head.o: reloc.S
-
-# NB. BOOT_TRAMPOLINE == 0x8c000
-%.S: %.c
-	RELOC=0x8c000 $(MAKE) -f build32.mk $@
+obj-y += reloc.o
xen/arch/x86/boot/reloc.c +9 -31
diff -Nur a/xen/arch/x86/boot/reloc.c b/xen/arch/x86/boot/reloc.c
@@ -9,43 +9,21 @@
  * Authors:
  *    Keir Fraser <[email protected]>
  */
-
-asm (
-    "    .text                         \n"
-    "    .globl _start                 \n"
-    "_start:                           \n"
-    "    mov  $_start,%edi             \n"
-    "    call 1f                       \n"
-    "1:  pop  %esi                     \n"
-    "    sub  $1b-_start,%esi          \n"
-    "    mov  $__bss_start-_start,%ecx \n"
-    "    rep  movsb                    \n"
-    "    xor  %eax,%eax                \n"
-    "    mov  $_end,%ecx               \n"
-    "    sub  %edi,%ecx                \n"
-    "    rep  stosb                    \n"
-    "    mov  $reloc,%eax              \n"
-    "    jmp  *%eax                    \n"
-    );
-
-typedef unsigned int u32;
+#include <asm/types.h>
+#include <xen/string.h>
+#include <asm/page.h>
 #include "../../../include/xen/multiboot.h"
 
-extern char _start[];
+#define sym_phys(sym) (unsigned long *)((unsigned long)(sym) - __XEN_VIRT_START)
 
-static void *memcpy(void *dest, const void *src, unsigned int n)
-{
-    char *s = (char *)src, *d = dest;
-    while ( n-- )
-        *d++ = *s++;
-    return dest;
-}
+static void *start = (void *)0x8c000;
 
 static void *reloc_mbi_struct(void *old, unsigned int bytes)
 {
-    static void *alloc = &_start;
-    alloc = (void *)(((unsigned long)alloc - bytes) & ~15ul);
-    return memcpy(alloc, old, bytes);
+    unsigned long alloc = *sym_phys(&start);
+    alloc = ((unsigned long)alloc - bytes) & ~15ul;
+    *sym_phys(&start) = alloc;
+    return memcpy((void *)alloc, old, bytes);
 }
 
 static char *reloc_mbi_string(char *old)
#1 lore raw
Quoted 11 lines · Xiao Guangrong

This patch simplify reloc.c with:
1): no need separate make
reloc.c -> reloc.o -> reloc.lnk -> reloc.bin -> reloc.S, and static embed reloc.S into head.S
2): reuse memcpy() in string lib
3): remove assemble code in the head of reloc.c

[ It is just a cleanup patch and not change the code's logic, and it work well on x86.
Not have x86_64 machine in my hand, thanks very much if someone can test it
on x86_64 ]

The patch misses the point of reloc.c, which is that it is supposed to run in 32-bit mode. All the stuff you stripped out is exactly what's needed to make it work with 64-bit Xen.

-- Keir

#2 lore raw
Quoted 9 lines · Keir Fraser
Quoted 7 lines · Xiao Guangrong

This patch simplify reloc.c with:
1): no need separate make
reloc.c -> reloc.o -> reloc.lnk -> reloc.bin -> reloc.S, and static embed reloc.S into head.S
2): reuse memcpy() in string lib
3): remove assemble code in the head of reloc.c

Quoted 9 lines
Quoted 4 lines

[ It is just a cleanup patch and not change the code's logic, and it work well on x86.
Not have x86_64 machine in my hand, thanks very much if someone can test it
on x86_64 ]

The patch misses the point of reloc.c, which is that it is supposed to run in 32-bit mode. All the stuff you stripped out is exactly what's needed to make it work with 64-bit Xen.

Ah, sorry for this noise, I'll do some work on x86-64 about this patch.

Thanks,
Xiao