1/*
2 * Copyright 2021 Haiku, Inc. All rights reserved.
3 * Released under the terms of the MIT License.
4 */
5
6
7#include <asm_defs.h>
8
9#include <arch/x86/descriptors.h>
10
11.text
12.code32
13
14/*
15extern "C" void arch_enter_kernel(uint32_t pageDirectory, addr_t kernelArgs,
16	addr_t kernelEntry, addr_t kernelStackTop,
17	struct gdt_idt_descr *gdtDescriptor);
18*/
19FUNCTION(arch_enter_kernel):
20
21	movl	4(%esp), %edx	// pageDirectory
22	movl	8(%esp), %ecx	// kernelArgs
23	movl	12(%esp), %ebx	// kernelEntry
24	movl	16(%esp), %eax	// kernelStackTop
25	movl	20(%esp), %esi	// gdtDescriptor
26
27	// initialize stack
28	movl	%eax, %esp
29
30	// ensure that Paging is disabled
31	movl	%cr0, %eax
32	andl	$0x7fffffff, %eax
33	movl	%eax, %cr0
34
35	// ensure that PAE is disabled
36	movl	%cr4, %eax
37	andl	$0xffffffdf, %eax
38	movl	%eax, %cr4
39
40	// set page directory
41	movl	%edx, %eax
42	movl	%eax, %cr3
43
44	// disable interrupts
45	cli
46
47	// clear direction flag
48	cld
49
50	// initialize floating point unit
51	fninit
52
53	movl	%esi, %eax
54	lgdt	(%eax)
55
56	// initialize CR0
57	// - bit #31: Enable Paging
58	// - bit #16: Write Protect
59	// - bit  #5: Numeric Error Handling
60	// - bit  #0: Protected Mode
61	movl	$0x80010021, %eax
62	movl	%eax, %cr0
63
64	// Set data segments.
65	movw	$KERNEL_DATA_SELECTOR, %ax
66	movw	%ax, %ss
67	movw	%ax, %ds
68	movw	%ax, %es
69	movw	%ax, %fs
70	movw	%ax, %gs
71
72	pushl	$0x0		// currentCpu
73	pushl	%ecx		// kernelArgs
74	pushl	$0x0		// fake return address
75	pushl	$KERNEL_CODE_SELECTOR
76	pushl	%ebx		// kernelEntry
77	lret
78
79	//return
80	movl	$-1, %eax
81	ret
82FUNCTION_END(arch_enter_kernel)
83