xref: /haiku/src/system/kernel/arch/x86/64/arch.S (revision efafab643ce980e3f3c916795ed302599f6b4f66)
1/*
2 * Copyright 2018, Jérôme Duval, jerome.duval@gmail.com.
3 * Copyright 2012, Alex Smith, alex@alex-smith.me.uk.
4 * Copyright 2003-2007, Axel Dörfler, axeld@pinc-software.de.
5 * Copyright 2012, Rene Gollent, rene@gollent.com.
6 * Distributed under the terms of the MIT License.
7 *
8 * Copyright 2001, Travis Geiselbrecht. All rights reserved.
9 * Copyright 2002, Michael Noisternig. All rights reserved.
10 * Distributed under the terms of the NewOS License.
11 */
12
13
14#include <asm_defs.h>
15
16#include "asm_offsets.h"
17#include "syscall_numbers.h"
18
19
20.text
21
22
23/* addr_t x86_get_stack_frame(); */
24FUNCTION(x86_get_stack_frame):
25	mov		%rbp, %rax
26	ret
27FUNCTION_END(x86_get_stack_frame)
28
29
30/* void x86_64_thread_entry(); */
31FUNCTION(x86_64_thread_entry):
32	xorq	%rbp, %rbp
33
34	movq	%rsp, %rax
35	addq	$16, %rsp
36	andq	$0xfffffffffffffff0, %rsp
37	subq	$8, %rsp
38
39	movq	8(%rax), %rdi
40	jmp		*(%rax)
41FUNCTION_END(x86_64_thread_entry)
42
43
44/* thread exit stub */
45.align 8
46FUNCTION(x86_userspace_thread_exit):
47	movq	%rax, %rdi
48	movq	$SYSCALL_EXIT_THREAD, %rax
49	syscall
50.align 8
51FUNCTION_END(x86_userspace_thread_exit)
52SYMBOL(x86_end_userspace_thread_exit):
53
54
55null_idt_descr:
56	.word	0
57	.quad	0
58
59FUNCTION(x86_reboot):
60	lidt	null_idt_descr
61	int		$0
62done:
63	jmp		done
64FUNCTION_END(x86_reboot)
65
66
67/*!	\fn void arch_debug_call_with_fault_handler(cpu_ent* cpu,
68		jmp_buf jumpBuffer, void (*function)(void*), void* parameter)
69
70	Called by debug_call_with_fault_handler() to do the dirty work of setting
71	the fault handler and calling the function. If the function causes a page
72	fault, the arch_debug_call_with_fault_handler() calls longjmp() with the
73	given \a jumpBuffer. Otherwise it returns normally.
74
75	debug_call_with_fault_handler() has already saved the CPU's fault_handler
76	and fault_handler_stack_pointer and will reset them later, so
77	arch_debug_call_with_fault_handler() doesn't need to care about it.
78
79	\param cpu The \c cpu_ent for the current CPU.
80	\param jumpBuffer Buffer to be used for longjmp().
81	\param function The function to be called.
82	\param parameter The parameter to be passed to the function to be called.
83*/
84FUNCTION(arch_debug_call_with_fault_handler):
85	push	%rbp
86	movq	%rsp, %rbp
87
88	// Preserve the jump buffer address for the fault return.
89	push	%rsi
90
91	// Set fault handler address, and fault handler stack pointer address. We
92	// don't need to save the previous values, since that's done by the caller.
93	movq	$.L_debug_call_fault_handler, CPU_ENT_fault_handler(%rdi)
94	movq	%rbp, CPU_ENT_fault_handler_stack_pointer(%rdi)
95
96	// Call the function.
97	movq	%rcx, %rdi
98	call	*%rdx
99
100	// Regular return.
101	movq	%rbp, %rsp
102	pop		%rbp
103	ret
104
105.L_debug_call_fault_handler:
106	// Fault -- return via longjmp(jumpBuffer, 1)
107	movq	%rbp, %rsp
108	movq	-8(%rsp), %rdi
109	movq	$1, %rsi
110	call	longjmp
111FUNCTION_END(arch_debug_call_with_fault_handler)
112
113
114	.section .rodata
115FUNCTION(_stac):
116	stac
117FUNCTION_END(_stac)
118
119FUNCTION(_clac):
120	clac
121FUNCTION_END(_clac)
122