2016-06-13 04:11:38 +00:00
|
|
|
;Functions preserve the registers rbx, rsp, rbp, r12, r13, r14, and 15
|
|
|
|
;rax, rdi, rsi, rdx, rcx, r8, r9, r10, r11 are scratch registers.
|
|
|
|
;function parameter: rdi,rsi,rdx,rcx,r8,r9
|
|
|
|
|
2019-06-26 05:47:18 +00:00
|
|
|
global arch_flush_gdt
|
|
|
|
global arch_flush_tlb
|
|
|
|
global arch_flush_idt
|
|
|
|
global arch_read_idt
|
|
|
|
global arch_read_cr3
|
|
|
|
global arch_write_cr3
|
|
|
|
global arch_read_cr8
|
|
|
|
global arch_write_cr8
|
|
|
|
global arch_cpuid
|
|
|
|
global arch_halt_cpu
|
|
|
|
global arch_read_msr
|
|
|
|
global arch_write_msr
|
2018-10-02 06:43:30 +00:00
|
|
|
|
|
|
|
|
|
|
|
section .text
|
|
|
|
bits 64
|
|
|
|
|
2019-11-28 18:02:52 +00:00
|
|
|
; uint64* arch_random_int64(void)
|
|
|
|
; Returns a random 64-bit integer
|
|
|
|
global arch_random_int64
|
|
|
|
arch_random_int64:
|
|
|
|
rdrand rax
|
|
|
|
ret
|
|
|
|
|
|
|
|
; uint32* arch_random_int32(void)
|
|
|
|
; Returns a random 32-bit integer
|
|
|
|
global arch_random_int32
|
|
|
|
arch_random_int32:
|
|
|
|
rdrand eax
|
|
|
|
ret
|
|
|
|
|
|
|
|
|
|
|
|
|
2019-06-26 05:47:18 +00:00
|
|
|
arch_flush_gdt:
|
2016-06-13 04:11:38 +00:00
|
|
|
push rbp
|
|
|
|
mov rbp,rsp
|
|
|
|
lgdt [rdi]
|
|
|
|
;reload cs
|
|
|
|
|
|
|
|
push rdx ; data_slct : ss
|
|
|
|
push rbp ; rsp
|
|
|
|
|
|
|
|
pushfq
|
|
|
|
pop rax
|
|
|
|
push rax ; eflags
|
|
|
|
|
|
|
|
push rsi ; cs
|
2017-02-03 07:08:43 +00:00
|
|
|
mov rax, .reload
|
|
|
|
push rax ;rip
|
2016-06-13 04:11:38 +00:00
|
|
|
iretq
|
|
|
|
.reload:
|
|
|
|
mov es,dx
|
|
|
|
mov fs,dx
|
|
|
|
mov gs,dx
|
|
|
|
mov ds,dx
|
|
|
|
pop rbp
|
|
|
|
ret
|
|
|
|
|
2018-10-02 06:43:30 +00:00
|
|
|
|
2019-06-26 05:47:18 +00:00
|
|
|
arch_flush_tlb:
|
2016-06-13 04:11:38 +00:00
|
|
|
mov rax,cr3
|
|
|
|
mov cr3,rax
|
|
|
|
ret
|
|
|
|
|
2018-10-02 06:43:30 +00:00
|
|
|
|
2019-06-26 05:47:18 +00:00
|
|
|
arch_flush_idt:
|
2016-06-13 04:11:38 +00:00
|
|
|
lidt [rdi]
|
|
|
|
ret
|
|
|
|
|
2016-06-14 06:33:31 +00:00
|
|
|
;======================
|
2019-06-26 05:47:18 +00:00
|
|
|
global arch_read_idt
|
|
|
|
arch_read_idt:
|
2016-06-14 06:33:31 +00:00
|
|
|
sidt [rdi]
|
|
|
|
ret
|
|
|
|
|
2016-06-13 04:11:38 +00:00
|
|
|
;======================
|
2019-06-26 05:47:18 +00:00
|
|
|
global arch_read_cr3
|
|
|
|
arch_read_cr3:
|
2016-06-13 04:11:38 +00:00
|
|
|
mov rax,cr3
|
|
|
|
ret
|
|
|
|
|
|
|
|
;======================
|
2019-06-26 05:47:18 +00:00
|
|
|
global arch_write_cr3
|
|
|
|
arch_write_cr3:
|
2016-06-13 04:11:38 +00:00
|
|
|
mov cr3,rdi
|
|
|
|
ret
|
|
|
|
|
2016-06-15 07:29:46 +00:00
|
|
|
;======================
|
2019-06-26 05:47:18 +00:00
|
|
|
global arch_read_cr8
|
|
|
|
arch_read_cr8:
|
2016-06-15 07:29:46 +00:00
|
|
|
mov rax,cr8
|
|
|
|
ret
|
|
|
|
|
|
|
|
;======================
|
2019-06-26 05:47:18 +00:00
|
|
|
global arch_write_cr8
|
|
|
|
arch_write_cr8:
|
2016-06-15 07:29:46 +00:00
|
|
|
mov cr8,rdi
|
|
|
|
ret
|
2016-06-13 04:11:38 +00:00
|
|
|
|
2019-06-26 05:47:18 +00:00
|
|
|
;======================
|
|
|
|
global arch_halt
|
|
|
|
arch_halt:
|
|
|
|
hlt
|
|
|
|
|
|
|
|
|
2016-06-13 04:11:38 +00:00
|
|
|
; ============================
|
2019-06-26 05:47:18 +00:00
|
|
|
; extern void KAPI arch_cpuid(uint32* eax, uint32* ebx, uint32* ecx, uint32* edx);
|
|
|
|
global arch_cpuid
|
|
|
|
arch_cpuid:
|
2016-06-13 04:11:38 +00:00
|
|
|
push rbp
|
|
|
|
mov rbp,rsp
|
|
|
|
; preserve rbx,rcx,rdx
|
|
|
|
push rbx
|
|
|
|
push rcx
|
|
|
|
push rdx
|
|
|
|
; cpuid parameters eax,ecx
|
|
|
|
mov eax, dword [rdi]
|
|
|
|
mov ecx, dword [rdx]
|
|
|
|
cpuid
|
|
|
|
; write results back to memory
|
|
|
|
mov dword [rdi], eax
|
|
|
|
mov dword [rsi], ebx
|
|
|
|
pop r11
|
|
|
|
mov dword [r11], ecx
|
|
|
|
pop r11
|
|
|
|
mov dword [r11], edx
|
|
|
|
pop rbx
|
|
|
|
mov rsp,rbp
|
|
|
|
pop rbp
|
|
|
|
ret
|
|
|
|
|
|
|
|
;====================
|
2019-06-26 05:47:18 +00:00
|
|
|
global arch_halt_cpu
|
|
|
|
arch_halt_cpu:
|
2016-06-13 04:11:38 +00:00
|
|
|
.loop:
|
|
|
|
hlt
|
|
|
|
jmp .loop
|
|
|
|
|
|
|
|
;====================
|
2018-03-24 00:58:24 +00:00
|
|
|
;(uint32 *ecx, uint32* edx, uint32* eax)
|
2019-06-26 05:47:18 +00:00
|
|
|
global arch_read_msr
|
|
|
|
arch_read_msr:
|
2016-06-13 04:11:38 +00:00
|
|
|
; preserve rdx
|
|
|
|
push rdx
|
|
|
|
mov ecx, dword [rdi]
|
|
|
|
rdmsr
|
|
|
|
mov dword [rdi], ecx
|
|
|
|
mov dword [rsi], edx
|
|
|
|
pop r11
|
|
|
|
mov dword [r11], eax
|
|
|
|
ret
|
|
|
|
|
|
|
|
;====================
|
2018-03-24 00:58:24 +00:00
|
|
|
;(uint32 *ecx, uint32* edx, uint32* eax)
|
2019-06-26 05:47:18 +00:00
|
|
|
global arch_write_msr
|
|
|
|
arch_write_msr:
|
2016-06-13 04:11:38 +00:00
|
|
|
mov ecx, dword [rdi]
|
|
|
|
mov eax, dword [rdx]
|
|
|
|
mov edx, dword [rsi]
|
|
|
|
wrmsr
|
2018-12-16 23:52:33 +00:00
|
|
|
ret
|
|
|
|
|
|
|
|
|
2019-06-26 05:47:18 +00:00
|
|
|
global arch_write_port_16
|
|
|
|
global arch_write_port_32
|
|
|
|
global arch_write_port_8
|
|
|
|
global arch_read_port_8
|
|
|
|
global arch_read_port_16
|
|
|
|
global arch_read_port_32
|
2018-12-16 23:52:33 +00:00
|
|
|
|
2019-06-26 05:47:18 +00:00
|
|
|
arch_write_port_32:
|
2018-12-16 23:52:33 +00:00
|
|
|
mov rdx,rdi
|
|
|
|
mov rax,rsi
|
|
|
|
out dx,eax
|
|
|
|
nop
|
|
|
|
nop
|
|
|
|
nop
|
|
|
|
ret
|
|
|
|
|
|
|
|
|
2019-06-26 05:47:18 +00:00
|
|
|
arch_write_port_16:
|
2018-12-16 23:52:33 +00:00
|
|
|
mov rdx,rdi
|
|
|
|
mov rax,rsi
|
|
|
|
out dx,ax
|
|
|
|
nop
|
|
|
|
nop
|
|
|
|
nop
|
|
|
|
ret
|
|
|
|
|
|
|
|
|
2019-06-26 05:47:18 +00:00
|
|
|
arch_write_port_8:
|
2018-12-16 23:52:33 +00:00
|
|
|
mov rdx,rdi
|
|
|
|
mov rax,rsi
|
|
|
|
out dx,al
|
|
|
|
nop
|
|
|
|
nop
|
|
|
|
nop
|
|
|
|
ret
|
|
|
|
|
2019-06-26 05:47:18 +00:00
|
|
|
arch_read_port_8:
|
2018-12-16 23:52:33 +00:00
|
|
|
mov rdx,rdi
|
|
|
|
xor rax,rax
|
|
|
|
in al,dx
|
|
|
|
nop
|
|
|
|
nop
|
|
|
|
nop
|
|
|
|
ret
|
|
|
|
|
2019-06-26 05:47:18 +00:00
|
|
|
arch_read_port_16:
|
2018-12-16 23:52:33 +00:00
|
|
|
mov rdx,rdi
|
|
|
|
xor rax,rax
|
|
|
|
in ax,dx
|
|
|
|
nop
|
|
|
|
nop
|
|
|
|
nop
|
|
|
|
ret
|
|
|
|
|
2019-06-26 05:47:18 +00:00
|
|
|
arch_read_port_32:
|
2018-12-16 23:52:33 +00:00
|
|
|
mov rdx,rdi
|
|
|
|
xor rax,rax
|
|
|
|
in eax,dx
|
|
|
|
nop
|
|
|
|
nop
|
|
|
|
nop
|
2016-06-13 04:11:38 +00:00
|
|
|
ret
|