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	// Align the stack before calling the function.
91	subq	$8, %rsp
92
93	// Set fault handler address, and fault handler stack pointer address. We
94	// don't need to save the previous values, since that's done by the caller.
95	movq	$.L_debug_call_fault_handler, CPU_ENT_fault_handler(%rdi)
96	movq	%rbp, CPU_ENT_fault_handler_stack_pointer(%rdi)
97
98	// Call the function.
99	movq	%rcx, %rdi
100	call	*%rdx
101
102	// Regular return.
103	movq	%rbp, %rsp
104	pop		%rbp
105	ret
106
107.L_debug_call_fault_handler:
108	// Fault -- return via longjmp(jumpBuffer, 1)
109	movq	%rbp, %rsp
110	movq	-8(%rsp), %rdi
111	movq	$1, %rsi
112	call	longjmp
113FUNCTION_END(arch_debug_call_with_fault_handler)
114
115
116	.section .rodata
117FUNCTION(_stac):
118	stac
119FUNCTION_END(_stac)
120
121FUNCTION(_clac):
122	clac
123FUNCTION_END(_clac)
124
125FUNCTION(_xsave):
126	xsave64		(%rdi)
127FUNCTION_END(_xsave)
128
129FUNCTION(_xsavec):
130	xsavec64	(%rdi)
131FUNCTION_END(_xsavec)
132
133FUNCTION(_xrstor):
134	xrstor64	(%rdi)
135FUNCTION_END(_xrstor)
136
137