Move assembly code out of the kernel
PATCH 1/1 Move assembly code out of the kernel
Hi,
Currently, most assembly code are in startup.S. This is normally used to ensure that the function address are below 1m, which is required if it would switch to real mode and call bios services. However, this make the kernel larger. For example, the biosdisk functions are only used by biodisk module, they should not be placed inside the kernel.
This patch support splitting such code from startup.S. For example, we create a new module biosdisk_stub.mod for assembly code of biosdisk. Instead of call prot_to_real and real_to_prot, we call grub_call_real_stub to enter real mode. grub_call_real_stub would copy the code to real mode and do the mode switch.
To avoid unnecessary memory transfer, grub_call_real_stub would not erase the real mode stub when it's done, so that it can be used directly next time. When the stub area is full, it zero it out and start anew. The area uses a simple verification method so that the old mapping is invalidated, the code would need be copied again on their next use.
The patch shows how to do it for the biosdisk module, here is the new grub_biosdisk_rw_int13_extensions function.
REAL_STUB_START(grub_biosdisk_rw_int13_extensions)
movb %dh, %ah movw %cx, %ds int $0x13 /* do the operation */ movb %ah, %dl /* save return value */ lret
REAL_STUB_END(grub_biosdisk_rw_int13_extensions)
FUNCTION(grub_biosdisk_rw_int13_extensions)
pushl %ebp pushl %esi /* compute the address of disk_address_packet */ movw %cx, %si xorw %cx, %cx shrl $4, %ecx /* save the segment to cx */ /* ah */ movb %al, %dh leal grub_biosdisk_rw_int13_extensions_stub, %eax call EXT_C(grub_call_real_stub) movb %dl, %al /* return value in %eax */ popl %esi popl %ebp ret
Real mode code is enclosed between REAL_STUB_START and REAL_STUB_END, no need to use .code16 and .code32 as it's handled by the macro. In the main function, use
leal grub_biosdisk_rw_int13_extensions_stub, %eax
call EXT_C(grub_call_real_stub)
to invoke grub_call_real_stub. grub_biosdisk_rw_int13_extensions_stub is defined in the REAL_STUB_START macro.
This same method can be applied to loaders, vbe, etc. In fact, almost all function behind grub_call_real_stub can be moved out of startup.S.
Signature · 1 line
Bean
conf/i386-pc.rmk +11 -1
@@ -154,7 +154,7 @@ grub_install_SOURCES = util/i386/pc/grub-install.in grub_mkrescue_SOURCES = util/i386/pc/grub-mkrescue.in # Modules. -pkglib_MODULES = biosdisk.mod _chain.mod _linux.mod linux.mod normal.mod \ +pkglib_MODULES = biosdisk.mod biosdisk_stub.mod _chain.mod _linux.mod linux.mod normal.mod \ _multiboot.mod chain.mod multiboot.mod reboot.mod halt.mod \ vbe.mod vbetest.mod vbeinfo.mod video.mod gfxterm.mod \ videotest.mod play.mod bitmap.mod tga.mod cpuid.mod serial.mod \
@@ -166,6 +166,16 @@ biosdisk_mod_SOURCES = disk/i386/pc/biosdisk.c biosdisk_mod_CFLAGS = $(COMMON_CFLAGS) biosdisk_mod_LDFLAGS = $(COMMON_LDFLAGS) +# For biosdisk_stub.mod. +biosdisk_stub_mod_SOURCES = disk/i386/pc/biosdisk_stub.S +biosdisk_stub_mod_CFLAGS = $(COMMON_CFLAGS) +biosdisk_stub_mod_LDFLAGS = $(COMMON_LDFLAGS) + +# For biosdiskstub.mod. +biosdiskstub_mod_SOURCES = disk/i386/pc/biosdisk.c +biosdiskstub_mod_CFLAGS = $(COMMON_CFLAGS) +biosdisk_mod_LDFLAGS = $(COMMON_LDFLAGS) + # For _chain.mod. _chain_mod_SOURCES = loader/i386/pc/chainloader.c _chain_mod_CFLAGS = $(COMMON_CFLAGS)
disk/i386/pc/biosdisk_stub.S new file +344 -0
344 lines not on this page · raw message
include/grub/i386/pc/biosdisk.h +7 -7
@@ -106,19 +106,19 @@ struct grub_biosdisk_dapgrub_uint64_t block; } __attribute__ ((packed)); -int EXPORT_FUNC (grub_biosdisk_rw_int13_extensions ) (int ah,int drive,void *dap); -int EXPORT_FUNC (grub_biosdisk_rw_standard ) (int ah,int drive,int coff,int hoff, +int grub_biosdisk_rw_int13_extensions (int ah,int drive,void *dap); +int grub_biosdisk_rw_standard (int ah,int drive,int coff,int hoff,int soff,int nsec,int segment); -int EXPORT_FUNC (grub_biosdisk_check_int13_extensions ) (int drive); -int EXPORT_FUNC (grub_biosdisk_get_diskinfo_int13_extensions ) (int drive, +int grub_biosdisk_check_int13_extensions (int drive); +int grub_biosdisk_get_diskinfo_int13_extensions (int drive,void *drp); -int EXPORT_FUNC (grub_biosdisk_get_cdinfo_int13_extensions ) (int drive, +int grub_biosdisk_get_cdinfo_int13_extensions (int drive,void *cdrp); -int EXPORT_FUNC (grub_biosdisk_get_diskinfo_standard ) (int drive, +int grub_biosdisk_get_diskinfo_standard (int drive,unsigned long *cylinders,unsigned long *heads,unsigned long *sectors); -int EXPORT_FUNC (grub_biosdisk_get_num_floppies ) (void ); +int grub_biosdisk_get_num_floppies (void );void grub_biosdisk_init (void );void grub_biosdisk_fini (void );
include/grub/i386/pc/kernel.h +2 -0
@@ -86,6 +86,8 @@ extern grub_addr_t grub_end_addr;extern grub_addr_t EXPORT_FUNC (grub_arch_memdisk_addr ) (void );extern grub_off_t EXPORT_FUNC (grub_arch_memdisk_size ) (void ); +void EXPORT_FUNC (grub_call_real_stub ) (void ); +#endif /* ! ASM_FILE */ #endif /* ! KERNEL_MACHINE_HEADER */
include/grub/i386/pc/memory.h +6 -1
@@ -45,12 +45,17 @@ (GRUB_MEMORY_MACHINE_SCRATCH_ADDR +GRUB_MEMORY_MACHINE_SCRATCH_SIZE \ + GRUB_MEMORY_MACHINE_PROT_STACK_SIZE - 0x10) +#define GRUB_MEMORY_MACHINE_REAL_STUB_START (GRUB_MEMORY_MACHINE_PROT_STACK + 0x10) +#define GRUB_MEMORY_MACHINE_REAL_STUB_SIZE 0x8000 +#define GRUB_MEMORY_MACHINE_REAL_STUB_END \ + (GRUB_MEMORY_MACHINE_REAL_STUB_START + GRUB_MEMORY_MACHINE_REAL_STUB_SIZE) +/* The memory area where GRUB uses its own purpose. This part is not added into free memory for dynamic allocations. */ #define GRUB_MEMORY_MACHINE_RESERVED_START \ GRUB_MEMORY_MACHINE_SCRATCH_ADDR#define GRUB_MEMORY_MACHINE_RESERVED_END \ - (GRUB_MEMORY_MACHINE_PROT_STACK + 0x10) + GRUB_MEMORY_MACHINE_REAL_STUB_END/* The area where GRUB is decompressed at early startup. */ #define GRUB_MEMORY_MACHINE_DECOMPRESSION_ADDR 0x100000
include/grub/symbol.h +10 -0
@@ -43,4 +43,14 @@# define EXPORT_VAR (x) x#endif /* ! GRUB_SYMBOL_GENERATOR */ +#define REAL_STUB_START (x) x ## _stub: ; \ + .long x ## _start ; \ + .long x ## _end - x ## _start ; \ + .long 0 ; \ +x ## _start: ; \ + .code16 + +#define REAL_STUB_END (x) .code32 ; \ +x ## _end: +#endif /* ! GRUB_SYMBOL_HEADER */
kern/i386/pc/startup.S +77 -300
434 lines not on this page · raw message
Quoted 58 lines · Bean
Hi,
Currently, most assembly code are in
startup.S. This is normally used to ensure that the function address are below 1m, which is required if it would switch to real mode and call bios services. However, this make the kernel larger. For example, the biosdisk functions are only used by biodisk module, they should not be placed inside the kernel.This patch support splitting such code from
startup.S. For example, we create a new module biosdisk_stub.mod for assembly code of biosdisk. Instead of callprot_to_realandreal_to_prot, we callgrub_call_real_stubto enter real mode.grub_call_real_stubwould copy the code to real mode and do the mode switch.To avoid unnecessary memory transfer,
grub_call_real_stubwould not erase the real mode stub when it's done, so that it can be used directly next time. When the stub area is full, it zero it out and start anew. The area uses a simple verification method so that the old mapping is invalidated, the code would need be copied again on their next use.The patch shows how to do it for the biosdisk module, here is the new
grub_biosdisk_rw_int13_extensionsfunction.
REAL_STUB_START(grub_biosdisk_rw_int13_extensions)movb %dh, %ah movw %cx, %ds int $0x13 /* do the operation */ movb %ah, %dl /* save return value */lret
REAL_STUB_END(grub_biosdisk_rw_int13_extensions)
FUNCTION(grub_biosdisk_rw_int13_extensions)
pushl %ebp
pushl %esi/* compute the address of
disk_address_packet*/
movw %cx, %si
xorw %cx, %cx
shrl $4, %ecx /* save the segment to cx *//* ah */
movb %al, %dhleal
grub_biosdisk_rw_int13_extensions_stub, %eax
callEXT_C(grub_call_real_stub)movb %dl, %al /* return value in %eax */
popl %esi
popl %ebpret
Real mode code is enclosed between
REAL_STUB_STARTandREAL_STUB_END, no need to use .code16 and .code32 as it's handled by the macro. In the main function, use
Quoted 8 lines
leal
grub_biosdisk_rw_int13_extensions_stub, %eax
callEXT_C(grub_call_real_stub)to invoke
grub_call_real_stub.grub_biosdisk_rw_int13_extensions_stubis defined in theREAL_STUB_STARTmacro.This same method can be applied to loaders, vbe, etc. In fact, almost all function behind
grub_call_real_stubcan be moved out ofstartup.S.
Hi,
I make some adjustment for the patch. First, I don't try to remap blocks anymore, it's not so reliable. and anyway, the real stub area is 0x8000 long, which is more than enough for the assembly code. If the area would ever overflow, I just halt grub2. This makes the code much simpler, and also, there is no need for an extra field to store the mapped address, we just replace the original address with the mapped one.
I also add a grub_alloc_real_stub function, which can be used to allocate memory from the stub area that can be used to to communicate with real mode service. Please note that once allocated, the space can't be released, so remember to save the pointer for later use.
Some real mode service need some kind of alignment for the input data, so I add two more function grub_call_real_stub_align and grub_alloc_real_stub_align, which have a alignment parameter. 1 means no alignment, 2 word alignment, 4 dword alignment, etc. Don't use 0, it will cause problem for the function.
2008-07-31 Bean <[email protected]>
* conf/i386-pc.rmk (biosdisk_mod_SOURCES): Add disk/i386/pc/biosdisk_stub.S.
* disk/i386/pc/biosdisk_stub.S: New file.
* include/grub/i386/pc/biosdisk.h (grub_biosdisk_rw_int13_extensions):
Remove EXPORT_FUNC, as we have moved it out of the kernel. (grub_biosdisk_rw_standard): Likewise. (grub_biosdisk_check_int13_extensions): Likewise. (grub_biosdisk_get_diskinfo_int13_extensions): Likewise. (grub_biosdisk_get_cdinfo_int13_extensions): Likewise. (grub_biosdisk_get_diskinfo_standard): Likewise. (grub_biosdisk_get_num_floppies): Likewise.
* include/grub/i386/pc/kernel.h (grub_real_stub): New structure. (grub_alloc_real_stub): New function. (grub_alloc_real_stub_align): Likewise. (grub_call_real_stub): Likewise. (grub_call_real_stub_align): Likewise.
* include/grub/i386/pc/memory.h (GRUB_MEMORY_MACHINE_REAL_STUB_START):
New macro.
(GRUB_MEMORY_MACHINE_REAL_STUB_SIZE): Likewise. (GRUB_MEMORY_MACHINE_REAL_STUB_END): Likewise. (GRUB_MEMORY_MACHINE_RESERVED_END): Change value.
* include/symbol.h (REAL_STUB_START): New macro. (REAL_STUB_END): Likewise.
* kern/i386/pc/startup.S (real_stub_addr): New variable. (grub_alloc_real_stub): New function. (grub_alloc_real_stub_align): Likewise. (grub_call_real_stub): Likewise. (grub_call_real_stub_align): Likewise. (grub_biosdisk_rw_int13_extensions): Moved to biosdisk_stub.S. (grub_biosdisk_rw_standard): Likewise. (grub_biosdisk_check_int13_extensions): Likewise. (grub_biosdisk_get_diskinfo_int13_extensions): Likewise. (grub_biosdisk_get_cdinfo_int13_extensions): Likewise. (grub_biosdisk_get_diskinfo_standard): Likewise. (grub_biosdisk_get_num_floppies): Likewise.
Signature · 1 line
Bean
conf/i386-pc.rmk +1 -1
@@ -162,7 +162,7 @@ pkglib_MODULES = biosdisk.mod _chain.mod _linux.mod linux.mod normal.mod \ aout.mod _bsd.mod bsd.mod pxe.mod pxecmd.mod # For biosdisk.mod. -biosdisk_mod_SOURCES = disk/i386/pc/biosdisk.c +biosdisk_mod_SOURCES = disk/i386/pc/biosdisk.c disk/i386/pc/biosdisk_stub.S biosdisk_mod_CFLAGS = $(COMMON_CFLAGS) biosdisk_mod_LDFLAGS = $(COMMON_LDFLAGS)
disk/i386/pc/biosdisk_stub.S new file +344 -0
344 lines not on this page · raw message
include/grub/i386/pc/biosdisk.h +7 -7
@@ -106,19 +106,19 @@ struct grub_biosdisk_dapgrub_uint64_t block; } __attribute__ ((packed)); -int EXPORT_FUNC (grub_biosdisk_rw_int13_extensions ) (int ah,int drive,void *dap); -int EXPORT_FUNC (grub_biosdisk_rw_standard ) (int ah,int drive,int coff,int hoff, +int grub_biosdisk_rw_int13_extensions (int ah,int drive,void *dap); +int grub_biosdisk_rw_standard (int ah,int drive,int coff,int hoff,int soff,int nsec,int segment); -int EXPORT_FUNC (grub_biosdisk_check_int13_extensions ) (int drive); -int EXPORT_FUNC (grub_biosdisk_get_diskinfo_int13_extensions ) (int drive, +int grub_biosdisk_check_int13_extensions (int drive); +int grub_biosdisk_get_diskinfo_int13_extensions (int drive,void *drp); -int EXPORT_FUNC (grub_biosdisk_get_cdinfo_int13_extensions ) (int drive, +int grub_biosdisk_get_cdinfo_int13_extensions (int drive,void *cdrp); -int EXPORT_FUNC (grub_biosdisk_get_diskinfo_standard ) (int drive, +int grub_biosdisk_get_diskinfo_standard (int drive,unsigned long *cylinders,unsigned long *heads,unsigned long *sectors); -int EXPORT_FUNC (grub_biosdisk_get_num_floppies ) (void ); +int grub_biosdisk_get_num_floppies (void );void grub_biosdisk_init (void );void grub_biosdisk_fini (void );
include/grub/i386/pc/kernel.h +13 -0
@@ -86,6 +86,19 @@ extern grub_addr_t grub_end_addr;extern grub_addr_t EXPORT_FUNC (grub_arch_memdisk_addr ) (void );extern grub_off_t EXPORT_FUNC (grub_arch_memdisk_size ) (void ); +struct grub_real_stub +{ +void *address; +grub_uint32_t length; +}; + +void *EXPORT_FUNC (grub_alloc_real_stub ) (int size); +void *EXPORT_FUNC (grub_alloc_real_stub_align ) (int size,int align); + +void EXPORT_FUNC (grub_call_real_stub ) (struct grub_real_stub *stub); +void EXPORT_FUNC (grub_call_real_stub_align ) (struct grub_real_stub *stub, +int align); +#endif /* ! ASM_FILE */ #endif /* ! KERNEL_MACHINE_HEADER */
include/grub/i386/pc/memory.h +6 -1
@@ -45,12 +45,17 @@ (GRUB_MEMORY_MACHINE_SCRATCH_ADDR +GRUB_MEMORY_MACHINE_SCRATCH_SIZE \ + GRUB_MEMORY_MACHINE_PROT_STACK_SIZE - 0x10) +#define GRUB_MEMORY_MACHINE_REAL_STUB_START (GRUB_MEMORY_MACHINE_PROT_STACK + 0x10) +#define GRUB_MEMORY_MACHINE_REAL_STUB_SIZE 0x8000 +#define GRUB_MEMORY_MACHINE_REAL_STUB_END \ + (GRUB_MEMORY_MACHINE_REAL_STUB_START + GRUB_MEMORY_MACHINE_REAL_STUB_SIZE) +/* The memory area where GRUB uses its own purpose. This part is not added into free memory for dynamic allocations. */ #define GRUB_MEMORY_MACHINE_RESERVED_START \ GRUB_MEMORY_MACHINE_SCRATCH_ADDR#define GRUB_MEMORY_MACHINE_RESERVED_END \ - (GRUB_MEMORY_MACHINE_PROT_STACK + 0x10) + GRUB_MEMORY_MACHINE_REAL_STUB_END/* The area where GRUB is decompressed at early startup. */ #define GRUB_MEMORY_MACHINE_DECOMPRESSION_ADDR 0x100000
include/grub/symbol.h +9 -0
@@ -43,4 +43,13 @@# define EXPORT_VAR (x) x#endif /* ! GRUB_SYMBOL_GENERATOR */ +#define REAL_STUB_START (x) x ## _stub: ; \ + .long x ## _start ; \ + .long x ## _end - x ## _start ; \ +x ## _start: ; \ + .code16 + +#define REAL_STUB_END (x) .code32 ; \ +x ## _end: +#endif /* ! GRUB_SYMBOL_HEADER */
kern/i386/pc/startup.S +90 -290
447 lines not on this page · raw message
Quoted 6 lines · Bean
include/grub/symbol.h +0
-0@@ -43,4 +43,13 @@# define EXPORT_VAR (x) x
include/grub/symbol.h line 53
Quoted 10 lines
include/grub/symbol.h +8
-0#endif /* ! GRUB_SYMBOL_GENERATOR */ +#define REAL_STUB_START (x) x ## _stub: ; \ + .long x ## _start ; \ + .long x ## _end - x ## _start ; \ +x ## _start: ; \ + .code16 + +#define REAL_STUB_END (x) .code32 ; \ +x ## _end:
Shouldn't this be in an i386/pc/ subdir?
include/grub/symbol.h
Quoted 12 lines
include/grub/symbol.h +12
-0+* On entry, %eax should points to a structure that specifies the code/data to +* be copied: +* +* offset 0: address of code/data +* offset 4: size of code/data +* +* grub_copy_real_stub fetches the address from offset 0, if it's not below 1M or +* properly aligned, it copies it to a reserved area and updates the address at +* offset 0, so that it don't need to do it again the next time. +* +* Currently, the reserved area for stub code/data is 0x80000 - 0x88000. It +* should be big enough for assembly code.
I wonder if this could be made simpler. For example, by having a function that just calls "int" in real mode. Then callers could use inline assembly to put parameters in appropiate registers and reading results from appropiate registers. Would this be faster than copiing hooks?
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."
Quoted 9 lines · Robert Millan
Quoted 8 lines · Bean
include/grub/symbol.h +0
-0@@ -43,4 +43,13 @@# define EXPORT_VAR (x) x#endif /* ! GRUB_SYMBOL_GENERATOR */
Quoted 10 lines
include/grub/symbol.h line 53
Quoted 8 lines
include/grub/symbol.h +8
-0+#define REAL_STUB_START (x) x ## _stub: ; \ + .long x ## _start ; \ + .long x ## _end - x ## _start ; \ +x ## _start: ; \ + .code16 + +#define REAL_STUB_END (x) .code32 ; \ +x ## _end:Shouldn't this be in an i386/pc/ subdir?
Oh, perhaps I can move them to include/i386/pc/kernel.h
Quoted 14 lines
include/grub/symbol.h
Quoted 13 lines
include/grub/symbol.h +12
-0+* On entry, %eax should points to a structure that specifies the code/data to +* be copied: +* +* offset 0: address of code/data +* offset 4: size of code/data +* +* grub_copy_real_stub fetches the address from offset 0, if it's not below 1M or +* properly aligned, it copies it to a reserved area and updates the address at +* offset 0, so that it don't need to do it again the next time. +* +* Currently, the reserved area for stub code/data is 0x80000 - 0x88000. It +* should be big enough for assembly code.
Quoted 4 lines
I wonder if this could be made simpler. For example, by having a function that just calls "int" in real mode. Then callers could use inline assembly to put parameters in appropiate registers and reading results from appropiate registers. Would this be faster than copiing hooks?
INT sounds good, but it can't cover all cases. For example, it can't pass parameter using stack, or call a far pointer. Besides, we still need the stub area to store data.
Signature · 1 line
Bean