Created
January 30, 2016 01:17
-
-
Save mike820324/2b7ea9c282de05e39d2c to your computer and use it in GitHub Desktop.
A simple goto ring3 code for unicorn
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| # GDTR address | |
| GDTR_ADDR = 0xc0001000 | |
| # user mode entry table | |
| # - entry point | |
| # - stack pointer | |
| ENTRY_TABLE = 0xc0002000 | |
| # RING0_CS selector | |
| KERNEL_CS_INDEX = 0x8 | |
| # RING3_DS selector | |
| USER_DS_INDEX = 0x1b | |
| .code32 | |
| .global kernel_start | |
| kernel_start: | |
| lgdt GDTR_ADDR; | |
| movl $0x174, %ecx; | |
| movl $KERNEL_CS_INDEX, %eax; | |
| xorl %edx, %edx; | |
| wrmsr; | |
| movl $ring3, %edx; | |
| # kernel sp will not be used | |
| movl $0x0, %ecx; | |
| sysexit; | |
| # now we are in ring 3, setup proper segement register | |
| # and we are ready to go | |
| ring3: | |
| xorl %eax, %eax; | |
| mov %ax, %fs; | |
| mov $USER_DS_INDEX, %ax; | |
| mov %ax, %ds; | |
| mov %ax, %gs; | |
| mov %ax, %es; | |
| movl $ENTRY_TABLE, %eax; | |
| movl 0x4(%eax), %esp; | |
| jmp *(%eax); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| OUTPUT_FORMAT("binary") | |
| INPUT(boot_32.o) | |
| ENTRY(kernel_start) | |
| OUTPUT(boot_32.bin) | |
| SECTIONS | |
| { | |
| .text 0xc0003000 : | |
| { | |
| *(.text) | |
| } | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| CC := gcc | |
| LD := ld | |
| ASFLAGS := -c | |
| LDFLAGS := -T link.ld | |
| all: boot_32.bin | |
| boot_32.bin: boot_32.o | |
| @${LD} ${LDFLAGS} | |
| @echo "LINK $@" | |
| boot_32.o: boot_32.S | |
| @${CC} ${ASFLAGS} -o $@ $< | |
| @echo "AS $@" | |
| @objcopy -j .text $@ | |
| @echo "STRIP $@" | |
| clean: | |
| @rm -v *.o *.bin 2>/dev/null |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment