summaryrefslogtreecommitdiff
path: root/src/cpu/x86/64bit/mode_switch.S
blob: c27f540ba31ac8e69cdaafa87bf6c52a42d99c4f (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
/* SPDX-License-Identifier: GPL-2.0-only */

.text
.code64
	.section ".text.protected_mode_call", "ax", @progbits
	.globl protected_mode_call_narg
protected_mode_call_narg:

	push	%rbp
	mov	%rsp, %rbp
	/* Preserve registers */
	push	%rbx
	push	%r12
	push	%r13
	push	%r14
	push	%r15

	/* Backup gs to stack */
	movl	%gs, %eax
	push	%rax

	/* Arguments to stack */
	push	%rdi
	push	%rsi
	push	%rdx
	push	%rcx

	#include <cpu/x86/64bit/exit32.inc>

	movl	-56(%ebp), %eax	/* Argument count */
	movl	-72(%ebp), %edx	/* Argument 0 */
	movl	-80(%ebp), %ecx	/* Argument 1 */

	/* Align the stack */
	andl	$0xFFFFFFF0, %esp
	test	%eax, %eax
	je	1f	/* Zero arguments */

	subl	$1, %eax
	test	%eax, %eax
	je	2f	/* One argument */

	/* Two arguments */
	subl	$8, %esp
	pushl	%ecx	/* Argument 1 */
	pushl	%edx	/* Argument 0 */
	jmp	1f
2:
	subl	$12, %esp
	pushl	%edx	/* Argument 0 */

1:
	movl	-64(%ebp), %ebx	/* Function to call */
	call	*%ebx
	movl	%eax, %ebx

	/* Preserves ebx */
	#include <cpu/x86/64bit/entry64.inc>

	/* Place return value in rax */
	movl	%ebx, %eax

	/* Restore registers */
	mov	-48(%rbp), %rbx
	movl	%ebx, %gs
	mov	-40(%rbp), %r15
	mov	-32(%rbp), %r14
	mov	-24(%rbp), %r13
	mov	-16(%rbp), %r12
	mov	-8(%rbp), %rbx

	/* Restore stack pointer */
	mov	%rbp, %rsp
	pop	%rbp

	ret