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