Skip to contentSkip to conversation

Move assembly code out of the kernel

PATCH [email protected] 2 participants 4 messages Jul 31, 2008 – Aug 24, 2008

lore
Share & tools
lore
#0 lore raw

Bean [[email protected]] kirjoitti:

Quoted 58 lines

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

Quoted 8 lines

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.

I have made generic function that does basically the same thing for bios service 0x10 (video). In that modification you prepare registers structure that will be configured during real mode switching. I am yet to commit it for review, but I think it would be more generic way to do this. When I come back from my holiday I will commit the code for review.

So please wait a bit before committing this :)

Thanks,
Vesa Jääskeläinen

#1 lore raw
Quoted 7 lines · [email protected] wrote:

I have made generic function that does basically the same thing for bios service 0x10 (video). In that modification you prepare registers structure that will be configured during real mode switching. I am yet to commit it for review, but I think it would be more generic way to do this. When I come back from my holiday I will commit the code for review.

So please wait a bit before committing this :)

And here it is :)

Comments are welcome.

i386-pc.rmk +4 -3
Index: conf/i386-pc.rmk
===================================================================
@@ -55,7 +55,7 @@
 	partition.h pc_partition.h rescue.h symbol.h term.h time.h types.h \
 	machine/biosdisk.h machine/boot.h machine/console.h machine/init.h \
 	machine/memory.h machine/loader.h machine/vga.h machine/vbe.h \
-	machine/kernel.h machine/pxe.h
+	machine/kernel.h machine/pxe.h machine/bioscall.h
 kernel_img_CFLAGS = $(COMMON_CFLAGS)
 kernel_img_ASFLAGS = $(COMMON_ASFLAGS)
 kernel_img_LDFLAGS = $(COMMON_LDFLAGS) $(TARGET_IMG_LDFLAGS) -Wl,-Ttext,$(GRUB_MEMORY_MACHINE_LINK_ADDR) $(COMMON_CFLAGS)
@@ -231,8 +231,9 @@
 multiboot_mod_LDFLAGS = $(COMMON_LDFLAGS)
 
 # For vbe.mod.
-vbe_mod_SOURCES = video/i386/pc/vbe.c video/i386/pc/vbeblit.c \
-		  video/i386/pc/vbefill.c video/i386/pc/vbeutil.c
+vbe_mod_SOURCES = video/i386/pc/vbe.c video/i386/pc/vbebios.c \
+		  video/i386/pc/vbeblit.c video/i386/pc/vbefill.c \
+		  video/i386/pc/vbeutil.c
 vbe_mod_CFLAGS = $(COMMON_CFLAGS)
 vbe_mod_LDFLAGS = $(COMMON_LDFLAGS)
 
i386/pc/startup.S +167 -431

607 lines not on this page · raw message

i386/pc/vbebios.c +251 -0

251 lines not on this page · raw message

Property changes on: video/i386/pc/vbebios.c
___________________________________________________________________
Name: svn:mime-type
+ text/plain

grub/i386/pc/vbe.h +20 -22

68 lines not on this page · raw message

grub/i386/pc/bioscall.h +44 -0

44 lines not on this page · raw message

Property changes on: include/grub/i386/pc/bioscall.h
___________________________________________________________________
Name: svn:mime-type
+ text/plain

ChangeLog +37 -0
Index: ChangeLog
===================================================================
@@ -1,3 +1,40 @@
+2008-08-17  Vesa Jääskeläinen  <[email protected]>
+
+	* conf/i386-pc.rmk (kernel_img_HEADERS): Add machine/bioscall.h.
+	(vbe_mod_SOURCES): Add video/i386/pc/vbebios.c.
+
+	* include/grub/i386/pc/bioscall.h: New file.
+
+	* include/grub/i386/pc/vbe.h (grub_vbe_bios_get_controller_info):
+	Moved from kernel to vbe.mod.
+	(grub_vbe_bios_get_mode_info): Likewise.
+	(grub_vbe_bios_set_mode): Likewise.
+	(grub_vbe_bios_get_mode): Likewise.
+	(grub_vbe_bios_set_memory_window): Likewise.
+	(grub_vbe_bios_get_memory_window): Likewise.
+	(grub_vbe_bios_set_scanline_length): Likewise.
+	(grub_vbe_bios_get_scanline_length): Likewise.
+	(grub_vbe_bios_set_display_start): Likewise.
+	(grub_vbe_bios_get_display_start): Likewise.
+	(grub_vbe_bios_set_palette_data): Likewise.
+
+	* kern/i386/pc/startup.s (grub_vbe_bios_get_controller_info):
+	Removed.
+	(grub_vbe_bios_get_controller_info): Likewise.
+	(grub_vbe_bios_get_mode_info): Likewise.
+	(grub_vbe_bios_set_mode): Likewise.
+	(grub_vbe_bios_get_mode): Likewise.
+	(grub_vbe_bios_set_memory_window): Likewise.
+	(grub_vbe_bios_get_memory_window): Likewise.
+	(grub_vbe_bios_set_scanline_length): Likewise.
+	(grub_vbe_bios_get_scanline_length): Likewise.
+	(grub_vbe_bios_set_display_start): Likewise.
+	(grub_vbe_bios_get_display_start): Likewise.
+	(grub_vbe_bios_set_palette_data): Likewise.
+	(grub_bioscall_int10h): New function.
+
+	* video/i386/pc/vbebios.c: New file.
+
 2008-08-17  Carles Pina i Estany  <[email protected]>
 
         * menu/normal.c (run_menu): Add Home and End keys in grub-menu.
#2 lore raw
Quoted 8 lines · Vesa Jääskeläinen
Quoted 7 lines · [email protected] wrote:

I have made generic function that does basically the same thing for bios service 0x10 (video). In that modification you prepare registers structure that will be configured during real mode switching. I am yet to commit it for review, but I think it would be more generic way to do this. When I come back from my holiday I will commit the code for review.

So please wait a bit before committing this :)

Hi,

I'm thinking perhaps we can make it more generic and support multiple INT in one function. One way to do this is not use INT, but retrieve the address from IVT and do a far call. On return, pop the flag on stack. This would also make it easily to support far call, In that case, we just don't pop the flag.

I also notice that you do the protected/real mode switch in grub_bioscall_int10h, why not use the prot_to_real and real_to_prot ? If you need to keep the value of %eax, you can store it in a structure and pass it using other register like %ecx.

Signature · 1 line
Bean
#3 lore raw
Quoted 10 lines · Bean
Quoted 8 lines · Vesa Jääskeläinen
Quoted 7 lines · [email protected] wrote:

I have made generic function that does basically the same thing for bios service 0x10 (video). In that modification you prepare registers structure that will be configured during real mode switching. I am yet to commit it for review, but I think it would be more generic way to do this. When I come back from my holiday I will commit the code for review.

So please wait a bit before committing this :)

Quoted 7 lines

Hi,

I'm thinking perhaps we can make it more generic and support multiple INT in one function. One way to do this is not use INT, but retrieve the address from IVT and do a far call. On return, pop the flag on stack. This would also make it easily to support far call, In that case, we just don't pop the flag.

Hmm. I have faint memory that this has been done previously somewhere :)... need to think this a bit more.

Quoted 4 lines

I also notice that you do the protected/real mode switch in grub_bioscall_int10h, why not use the prot_to_real and real_to_prot ? If you need to keep the value of %eax, you can store it in a structure and pass it using other register like %ecx.

I wanted it to be easily manageable so I can alter everything in order to get it working at the first :)

But basically the points were:
- I needed to put registers to the stack and thus need to differentiate esp and ebp.
- When returning I need to store those registers to structure provided by caller.
- There was no need to "corrupt" stack area with return address, and I needed stack for different purpose

Thanks,
Vesa Jääskeläinen