c357504735b4f8e1fbe4a0d9f5102db9cf8e67bf
[coreboot.git] / src / cpu / i386 / entry16.inc
1 /*
2 This software and ancillary information (herein called SOFTWARE )
3 called LinuxBIOS          is made available under the terms described
4 here.  The SOFTWARE has been approved for release with associated
5 LA-CC Number 00-34   .  Unless otherwise indicated, this SOFTWARE has
6 been authored by an employee or employees of the University of
7 California, operator of the Los Alamos National Laboratory under
8 Contract No. W-7405-ENG-36 with the U.S. Department of Energy.  The
9 U.S. Government has rights to use, reproduce, and distribute this
10 SOFTWARE.  The public may copy, distribute, prepare derivative works
11 and publicly display this SOFTWARE without charge, provided that this
12 Notice and any statement of authorship are reproduced on all copies.
13 Neither the Government nor the University makes any warranty, express
14 or implied, or assumes any liability or responsibility for the use of
15 this SOFTWARE.  If SOFTWARE is modified to produce derivative works,
16 such modified SOFTWARE should be clearly marked, so as not to confuse
17 it with the version available from LANL.
18  */
19 /* Copyright 2000, Ron Minnich, Advanced Computing Lab, LANL
20  * rminnich@lanl.gov
21  */
22
23
24 /** Start code to put an i386 or later processor into 32-bit 
25  * protected mode. 
26  */
27
28 /* .section ".rom.text" */
29 #include <arch/rom_segs.h>
30 .code16
31 .globl  EXT(_start)
32 .type EXT(_start), @function
33
34 EXT(_start): 
35         cli
36
37 /* thanks to kmliu@sis.tw.com for this TBL fix ... */
38 /**/
39 /* IMMEDIATELY invalidate the translation lookaside buffer before executing*/
40 /* any further code.  Even though paging is disabled we could still get*/
41 /*false address translations due to the TLB if we didn't invalidate it.*/
42 /**/
43         xorl    %eax, %eax
44         movl    %eax, %cr3    /* Invalidate TLB*/
45
46         /* invalidate the cache */
47         invd 
48
49         /* Note: gas handles memory addresses in 16 bit code very poorly.
50          * In particular it doesn't appear to have a directive allowing you
51          * associate a section or even an absolute offset with a segment register.
52          *
53          * This means that anything except cs:ip relative offsets are
54          * a real pain in 16 bit mode.  And explains why it is almost
55          * imposible to get gas to do lgdt correctly.
56          *
57          * One way to work around this is to have the linker do the
58          * math instead of the assembler.  This solves the very
59          * pratical problem of being able to write code that can
60          * be relocated.
61          *
62          * An lgdt call before we have memory enabled cannot be 
63          * position independent, as we cannot execute a call
64          * instruction to get our current instruction pointer.
65          * So while this code is relocateable it isn't arbitrarily
66          * relocatable.
67          *
68          * The criteria for relocation have been relaxed to their 
69          * utmost, so that we can use the same code for both
70          * our initial entry point and startup of the second cpu.
71          * The code assumes when executing at _start that:
72          * (((cs & 0xfff) == 0) and (ip == _start & 0xffff))
73          * or
74          * ((cs == anything) and (ip == 0)).
75          *
76          * The restrictions in reset16.inc mean that _start initially
77          * must be loaded at or above 0xffff0000 or below 0x100000.
78          *
79          * The linker scripts computs gdtptr16_offset by simply returning
80          * the low 16 bits.  This means that the intial segment used
81          * when start is called must be 64K aligned.  This should not
82          * restrict the address as the ip address can be anything.
83          */
84
85         movw    %cs, %ax
86         shlw    $4, %ax
87         movw    $EXT(gdtptr16_offset), %bx
88         subw    %ax, %bx
89         data32  lgdt %cs:(%bx)
90
91         movl    %cr0, %eax
92         andl    $0x7FFAFFD1, %eax /* PG,AM,WP,NE,TS,EM,MP = 0 */
93         orl     $0x60000001, %eax /* CD, NW, PE = 1 */
94         movl    %eax, %cr0
95
96         /* Now that we are in protected mode jump to a 32 bit code segment. */
97         data32  ljmp    $ROM_CODE_SEG, $__protected_start
98
99 /** The gdt has a 4 Gb code segment at 0x10, and a 4 GB data segment
100  * at 0x18; these are Linux-compatible. 
101  */
102
103 .align  4
104 .globl EXT(gdtptr16)
105 EXT(gdtptr16):
106         .word   gdt_end - gdt -1 /* compute the table limit */
107         .long   gdt              /* we know the offset */
108
109 .globl EXT(_estart)
110 EXT(_estart):
111         .code32
112