Home | History | Annotate | Line # | Download | only in bootia32
startprog32.S revision 1.1
      1 /*	$NetBSD: startprog32.S,v 1.1 2017/02/21 10:53:37 nonaka Exp $	*/
      2 /*	NetBSD: startprog.S,v 1.4 2016/12/04 08:21:08 maxv Exp	*/
      3 
      4 /*
      5  * Ported to boot 386BSD by Julian Elischer (julian (at) tfs.com) Sept 1992
      6  *
      7  * Mach Operating System
      8  * Copyright (c) 1992, 1991 Carnegie Mellon University
      9  * All Rights Reserved.
     10  *
     11  * Permission to use, copy, modify and distribute this software and its
     12  * documentation is hereby granted, provided that both the copyright
     13  * notice and this permission notice appear in all copies of the
     14  * software, derivative works or modified versions, and any portions
     15  * thereof, and that both notices appear in supporting documentation.
     16  *
     17  * CARNEGIE MELLON ALLOWS FREE USE OF THIS SOFTWARE IN ITS "AS IS"
     18  * CONDITION.  CARNEGIE MELLON DISCLAIMS ANY LIABILITY OF ANY KIND FOR
     19  * ANY DAMAGES WHATSOEVER RESULTING FROM THE USE OF THIS SOFTWARE.
     20  *
     21  * Carnegie Mellon requests users of this software to return to
     22  *
     23  *  Software Distribution Coordinator  or  Software.Distribution (at) CS.CMU.EDU
     24  *  School of Computer Science
     25  *  Carnegie Mellon University
     26  *  Pittsburgh PA 15213-3890
     27  *
     28  * any improvements or extensions that they make and grant Carnegie Mellon
     29  * the rights to redistribute these changes.
     30  */
     31 
     32 /*
     33  *   Copyright 1988, 1989, 1990, 1991, 1992
     34  *    by Intel Corporation, Santa Clara, California.
     35  *
     36  *                 All Rights Reserved
     37  *
     38  * Permission to use, copy, modify, and distribute this software and
     39  * its documentation for any purpose and without fee is hereby
     40  * granted, provided that the above copyright notice appears in all
     41  * copies and that both the copyright notice and this permission notice
     42  * appear in supporting documentation, and that the name of Intel
     43  * not be used in advertising or publicity pertaining to distribution
     44  * of the software without specific, written prior permission.
     45  *
     46  * INTEL DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE
     47  * INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS,
     48  * IN NO EVENT SHALL INTEL BE LIABLE FOR ANY SPECIAL, INDIRECT, OR
     49  * CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
     50  * LOSS OF USE, DATA OR PROFITS, WHETHER IN ACTION OF CONTRACT,
     51  * NEGLIGENCE, OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION
     52  * WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
     53  */
     54 
     55 #include <machine/asm.h>
     56 #include <machine/specialreg.h>
     57 
     58 #define	CODE_SEGMENT	0x08
     59 #define	DATA_SEGMENT	0x10
     60 
     61 	.align	16
     62 	.globl _C_LABEL(startprog32)
     63 _C_LABEL(startprog32):
     64 	.quad 0
     65 
     66 	.globl _C_LABEL(startprog32_size)
     67 _C_LABEL(startprog32_size):
     68 	.long startprog32_end - _C_LABEL(startprog32_start)
     69 
     70 	.text
     71 	.p2align 4,,15
     72 
     73 /*
     74  * startprog32(entry,argc,argv,stack,kern_start,kern_load,kern_size,loadaddr)
     75  */
     76 ENTRY(startprog32_start)
     77 start:
     78 	pushl	%ebp
     79 	movl	%esp, %ebp
     80 
     81 	/*
     82 	 * 8(%ebp): kernel entry address
     83 	 * 12(%ebp): argc
     84 	 * 16(%ebp): argv
     85 	 * 20(%ebp): stack address
     86 	 * 24(%ebp): kernel start address
     87 	 * 28(%ebp): loaded kernel address
     88 	 * 32(%ebp): loaded kernel size
     89 	 * 36(%ebp): loaded start address
     90 	 */
     91 
     92 	cli
     93 
     94 	/* Prepare a new stack */
     95 	movl	20(%ebp), %eax	/* stack */
     96 	subl	$4, %eax
     97 	movl	%eax, %edi
     98 
     99 	/* Push some number of args onto the stack */
    100 	movl	12(%ebp), %ecx	/* argc */
    101 	movl	%ecx, %eax
    102 	decl	%eax
    103 	shl	$2, %eax
    104 	addl	16(%ebp), %eax	/* ptr to last arg */
    105 	movl	%eax, %esi
    106 
    107 	std			/* backwards */
    108 	rep
    109 	movsl			/* copy %ds:(%esi) -> %es:(%edi) */
    110 	cld
    111 	mov	%edi, %edx	/* %edx: new stack pointer */
    112 
    113 	/* Copy kernel */
    114 	movl	24(%esp), %edi	/* dest */
    115 	movl	28(%esp), %esi	/* src */
    116 	movl	32(%esp), %ecx	/* size */
    117 #if defined(NO_OVERLAP)
    118 	movl	%ecx, %eax
    119 #else
    120 	movl	%edi, %eax
    121 	subl	%esi, %eax
    122 	cmpl	%ecx, %eax	/* overlapping? */
    123 	movl	%ecx, %eax
    124 	jb	.Lbackwards
    125 #endif
    126 	/* nope, copy forwards. */
    127 	shrl	$2, %ecx	/* copy by words */
    128 	rep
    129 	movsl
    130 	and	$3, %eax	/* any bytes left? */
    131 	jnz	.Ltrailing
    132 	jmp	.Lcopy_done
    133 
    134 .Ltrailing:
    135 	cmp	$2, %eax
    136 	jb	1f
    137 	movw	(%esi), %ax
    138 	movw	%ax, (%edi)
    139 	je	.Lcopy_done
    140 	movb	2(%esi), %al
    141 	movb	%al, 2(%edi)
    142 	jmp	.Lcopy_done
    143 1:	movb	(%esi), %al
    144 	movb	%al, (%edi)
    145 	jmp	.Lcopy_done
    146 
    147 #if !defined(NO_OVERLAP)
    148 .Lbackwards:
    149 	addl	%ecx, %edi	/* copy backwards. */
    150 	addl	%ecx, %esi
    151 	and	$3, %eax	/* any fractional bytes? */
    152 	jnz	.Lback_align
    153 .Lback_aligned:
    154 	shrl	$2, %ecx
    155 	subl	$4, %esi
    156 	subl	$4, %edi
    157 	std
    158 	rep
    159 	movsl
    160 	cld
    161 	jmp	.Lcopy_done
    162 
    163 .Lback_align:
    164 	sub	%eax, %esi
    165 	sub	%eax, %edi
    166 	cmp	$2, %eax
    167 	jb	1f
    168 	je	2f
    169 	movb	2(%esi), %al
    170 	movb	%al, 2(%edi)
    171 2:	movw	(%esi), %ax
    172 	movw	%ax, (%edi)
    173 	jmp	.Lback_aligned
    174 1:	movb	(%esi), %al
    175 	movb	%al, (%edi)
    176 	jmp	.Lback_aligned
    177 #endif
    178 	/* End of copy kernel */
    179 .Lcopy_done:
    180 	cld			/* LynxOS depends on it */
    181 
    182 	movl	8(%ebp), %esi	/* %esi: entry address */
    183 	movl	36(%ebp), %edi	/* %edi: loaded start address */
    184 
    185 	/* Prepare jump address */
    186 	lea	(start32a - start)(%edi), %eax
    187 	movl	%eax, (start32r - start)(%edi)
    188 
    189 	/* Setup GDT */
    190 	lea	(gdt - start)(%edi), %eax
    191 	movl	%eax, (gdtrr - start)(%edi)
    192 	lgdt	(gdtr - start)(%edi)
    193 
    194 	/* Jump to set %cs */
    195 	ljmp	*(start32r - start)(%edi)
    196 
    197 	.align	4
    198 start32a:
    199 	movl	$DATA_SEGMENT, %eax
    200 	movw	%ax, %ds
    201 	movw	%ax, %es
    202 	movw	%ax, %fs
    203 	movw	%ax, %gs
    204 	movw	%ax, %ss
    205 
    206 	movl	%edx, %esp
    207 
    208 	/* Disable Paging in CR0 */
    209 	movl	%cr0, %eax
    210 	andl	$(~CR0_PG), %eax
    211 	movl	%eax, %cr0
    212 
    213 	/* Disable PAE in CR4 */
    214 	movl	%cr4, %eax
    215 	andl	$(~CR4_PAE), %eax
    216 	movl	%eax, %cr4
    217 
    218 	jmp	start32b
    219 
    220 	.align	4
    221 start32b:
    222 	xor	%eax, %eax
    223 	movl	%esi, (start32r - start)(%edi)
    224 	ljmp	*(start32r - start)(%edi)
    225 
    226 	.align	16
    227 start32r:
    228 	.long	0
    229 	.long	CODE_SEGMENT
    230 	.align	16
    231 gdt:
    232 	.long	0, 0
    233 	.byte	0xff, 0xff, 0x00, 0x00, 0x00, 0x9f, 0xcf, 0x00
    234 	.byte	0xff, 0xff, 0x00, 0x00, 0x00, 0x93, 0xcf, 0x00
    235 gdtr:
    236 	.word	gdtr - gdt
    237 gdtrr:
    238 	.quad
    239 start32end:
    240 	/* Space for the stack */
    241 	.align	16
    242 	.space	8192
    243 startprog32_end:
    244