Skip to content

Instantly share code, notes, and snippets.

@mike820324
Created January 30, 2016 01:17
Show Gist options
  • Select an option

  • Save mike820324/2b7ea9c282de05e39d2c to your computer and use it in GitHub Desktop.

Select an option

Save mike820324/2b7ea9c282de05e39d2c to your computer and use it in GitHub Desktop.
A simple goto ring3 code for unicorn
# 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);
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