startprog64.S revision 1.2.2.2 1 /* $NetBSD: startprog64.S,v 1.2.2.2 2017/02/05 13:40:13 skrll Exp $ */
2 /* NetBSD: startprog.S,v 1.3 2003/02/01 14:48:18 dsl Exp */
3
4 /* starts program in protected mode / flat space
5 with given stackframe
6 needs global variables flatcodeseg and flatdataseg
7 (gdt offsets)
8 derived from: NetBSD:sys/arch/i386/boot/asm.S
9 */
10
11 /*
12 * Ported to boot 386BSD by Julian Elischer (julian (at) tfs.com) Sept 1992
13 *
14 * Mach Operating System
15 * Copyright (c) 1992, 1991 Carnegie Mellon University
16 * All Rights Reserved.
17 *
18 * Permission to use, copy, modify and distribute this software and its
19 * documentation is hereby granted, provided that both the copyright
20 * notice and this permission notice appear in all copies of the
21 * software, derivative works or modified versions, and any portions
22 * thereof, and that both notices appear in supporting documentation.
23 *
24 * CARNEGIE MELLON ALLOWS FREE USE OF THIS SOFTWARE IN ITS "AS IS"
25 * CONDITION. CARNEGIE MELLON DISCLAIMS ANY LIABILITY OF ANY KIND FOR
26 * ANY DAMAGES WHATSOEVER RESULTING FROM THE USE OF THIS SOFTWARE.
27 *
28 * Carnegie Mellon requests users of this software to return to
29 *
30 * Software Distribution Coordinator or Software.Distribution (at) CS.CMU.EDU
31 * School of Computer Science
32 * Carnegie Mellon University
33 * Pittsburgh PA 15213-3890
34 *
35 * any improvements or extensions that they make and grant Carnegie Mellon
36 * the rights to redistribute these changes.
37 */
38
39 /*
40 Copyright 1988, 1989, 1990, 1991, 1992
41 by Intel Corporation, Santa Clara, California.
42
43 All Rights Reserved
44
45 Permission to use, copy, modify, and distribute this software and
46 its documentation for any purpose and without fee is hereby
47 granted, provided that the above copyright notice appears in all
48 copies and that both the copyright notice and this permission notice
49 appear in supporting documentation, and that the name of Intel
50 not be used in advertising or publicity pertaining to distribution
51 of the software without specific, written prior permission.
52
53 INTEL DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE
54 INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS,
55 IN NO EVENT SHALL INTEL BE LIABLE FOR ANY SPECIAL, INDIRECT, OR
56 CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
57 LOSS OF USE, DATA OR PROFITS, WHETHER IN ACTION OF CONTRACT,
58 NEGLIGENCE, OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION
59 WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
60 */
61
62 #include <machine/asm.h>
63 #include <machine/specialreg.h>
64
65 #define CODE_SEGMENT 0x08
66 #define DATA_SEGMENT 0x10
67
68 .align 16
69 .globl _C_LABEL(startprog64)
70 _C_LABEL(startprog64):
71 .quad 0
72
73 .globl _C_LABEL(startprog64_size)
74 _C_LABEL(startprog64_size):
75 .long startprog64_end - _C_LABEL(startprog64_start)
76
77 .text
78 .p2align 4,,15
79
80 /*
81 * startprog64(loadddr,entry,stack)
82 */
83 ENTRY(startprog64_start)
84 start:
85 /*
86 * This function is to call the loaded kernel's start() with
87 * 32bit segment mode from x64 mode.
88 * %rdi: loaded start address
89 * %rsi: kernel entry address
90 * %rdx: stack address
91 */
92
93 cld # LynxOS depends on it
94
95 /* Prepare jump address */
96 lea (start32a - start)(%rdi), %rax
97 movl %eax, (start32r - start)(%rdi)
98
99 cli
100
101 /* Setup GDT */
102 lea (gdt - start)(%rdi), %rax
103 mov %rax, (gdtrr - start)(%rdi)
104 lgdt (gdtr - start)(%rdi)
105
106 /* Jump to set %cs */
107 ljmp *(start32r - start)(%rdi)
108
109 .align 4
110 .code32
111 start32a:
112 movl $DATA_SEGMENT, %eax
113 movw %eax, %ds
114 movw %eax, %es
115 movw %eax, %fs
116 movw %eax, %gs
117 movw %eax, %ss
118
119 movl %edx, %esp
120
121 /* Disable Paging in CR0 */
122 movl %cr0, %eax
123 andl $(~CR0_PG), %eax
124 movl %eax, %cr0
125
126 /* Disable PAE in CR4 */
127 movl %cr4, %eax
128 andl $(~CR4_PAE), %eax
129 movl %eax, %cr4
130
131 jmp start32b
132
133 .align 4
134 start32b:
135 xor %eax, %eax
136 call *%esi
137
138 .align 16
139 start32r:
140 .long 0
141 .long CODE_SEGMENT
142 .align 16
143 gdt:
144 .long 0, 0
145 .byte 0xff, 0xff, 0x00, 0x00, 0x00, 0x9f, 0xcf, 0x00
146 .byte 0xff, 0xff, 0x00, 0x00, 0x00, 0x93, 0xcf, 0x00
147 gdtr:
148 .word gdtr - gdt
149 gdtrr:
150 .quad
151 start32end:
152 /* Space for the stack */
153 .align 16
154 .space 8192
155 startprog64_end:
156