blob: 55d9bd97e2f5adfedbb33e3c719e5494a5360ee7 [file] [log] [blame]
Eric Biedermanfcd5ace2004-10-14 19:29:29 +00001/*
Kyösti Mälkki0dbfb542011-11-22 20:21:06 +02002 * This software and ancillary information (herein called SOFTWARE)
3 * called LinuxBIOS is made available under the terms described here.
4 *
5 * The SOFTWARE has been approved for release with associated
6 * LA-CC Number 00-34. Unless otherwise indicated, this SOFTWARE has
7 * been authored by an employee or employees of the University of
8 * California, operator of the Los Alamos National Laboratory under
9 * Contract No. W-7405-ENG-36 with the U.S. Department of Energy.
10 *
11 * The U.S. Government has rights to use, reproduce, and distribute this
12 * SOFTWARE. The public may copy, distribute, prepare derivative works
13 * and publicly display this SOFTWARE without charge, provided that this
14 * Notice and any statement of authorship are reproduced on all copies.
15 *
16 * Neither the Government nor the University makes any warranty, express
17 * or implied, or assumes any liability or responsibility for the use of
18 * this SOFTWARE. If SOFTWARE is modified to produce derivative works,
19 * such modified SOFTWARE should be clearly marked, so as not to confuse
20 * it with the version available from LANL.
21 *
22 * Copyright (C) 2000, Ron Minnich rminnich@lanl.gov
23 * Advanced Computing Lab, LANL
Eric Biedermanfcd5ace2004-10-14 19:29:29 +000024 */
25
26
Kyösti Mälkki0dbfb542011-11-22 20:21:06 +020027/* Start code to put an i386 or later processor into 32-bit protected mode.
Eric Biedermanfcd5ace2004-10-14 19:29:29 +000028 */
29
Eric Biedermanfcd5ace2004-10-14 19:29:29 +000030#include <arch/rom_segs.h>
Kyösti Mälkki34856572019-01-09 20:30:52 +020031
32#if IS_ENABLED(CONFIG_SIPI_VECTOR_IN_ROM)
33/* Symbol _start16bit must be aligned to 4kB to start AP CPUs with
34 * Startup IPI message without RAM.
35 */
36.align 4096
37#endif
Eric Biedermanfcd5ace2004-10-14 19:29:29 +000038.code16
Aaron Durbinf8468d42016-03-02 14:47:37 -060039.globl _start16bit
40.type _start16bit, @function
Eric Biedermanfcd5ace2004-10-14 19:29:29 +000041
Aaron Durbinf8468d42016-03-02 14:47:37 -060042_start16bit:
Eric Biedermanfcd5ace2004-10-14 19:29:29 +000043 cli
44 /* Save the BIST result */
45 movl %eax, %ebp
Martin Roth14554372015-11-12 14:02:42 -070046#if !IS_ENABLED(CONFIG_NO_EARLY_BOOTBLOCK_POSTCODES)
Kyösti Mälkki2a40ebc2011-11-21 08:16:20 +020047 post_code(POST_RESET_VECTOR_CORRECT)
Martin Roth14554372015-11-12 14:02:42 -070048#endif
Kyösti Mälkki2a40ebc2011-11-21 08:16:20 +020049
Kyösti Mälkki0dbfb542011-11-22 20:21:06 +020050 /* IMMEDIATELY invalidate the translation lookaside buffer (TLB) before
51 * executing any further code. Even though paging is disabled we
52 * could still get false address translations due to the TLB if we
53 * didn't invalidate it. Thanks to kmliu@sis.com.tw for this TLB fix.
54 */
55
Eric Biedermanfcd5ace2004-10-14 19:29:29 +000056 xorl %eax, %eax
57 movl %eax, %cr3 /* Invalidate TLB*/
58
Eric Biedermanfcd5ace2004-10-14 19:29:29 +000059 /* Invalidating the cache here seems to be a bad idea on
60 * modern processors. Don't.
61 * If we are hyperthreaded or we have multiple cores it is bad,
62 * for SMP startup. On Opterons it causes a 5 second delay.
63 * Invalidating the cache was pure paranoia in any event.
Paul Menzel39851122018-02-14 15:13:38 +010064 * If your CPU needs it you can write a CPU dependent version of
Eric Biedermanfcd5ace2004-10-14 19:29:29 +000065 * entry16.inc.
66 */
67
68 /* Note: gas handles memory addresses in 16 bit code very poorly.
69 * In particular it doesn't appear to have a directive allowing you
70 * associate a section or even an absolute offset with a segment register.
71 *
72 * This means that anything except cs:ip relative offsets are
73 * a real pain in 16 bit mode. And explains why it is almost
Vikram Narayanan15370ca2012-01-21 20:19:14 +053074 * impossible to get gas to do lgdt correctly.
Eric Biedermanfcd5ace2004-10-14 19:29:29 +000075 *
76 * One way to work around this is to have the linker do the
77 * math instead of the assembler. This solves the very
78 * pratical problem of being able to write code that can
79 * be relocated.
80 *
Stefan Reinauer14e22772010-04-27 06:56:47 +000081 * An lgdt call before we have memory enabled cannot be
Eric Biedermanfcd5ace2004-10-14 19:29:29 +000082 * position independent, as we cannot execute a call
83 * instruction to get our current instruction pointer.
84 * So while this code is relocateable it isn't arbitrarily
85 * relocatable.
86 *
Stefan Reinauer14e22772010-04-27 06:56:47 +000087 * The criteria for relocation have been relaxed to their
Eric Biedermanfcd5ace2004-10-14 19:29:29 +000088 * utmost, so that we can use the same code for both
Elyes HAOUASd6e96862016-08-21 10:12:15 +020089 * our initial entry point and startup of the second CPU.
Aaron Durbinf8468d42016-03-02 14:47:37 -060090 * The code assumes when executing at _start16bit that:
91 * (((cs & 0xfff) == 0) and (ip == _start16bit & 0xffff))
Eric Biedermanfcd5ace2004-10-14 19:29:29 +000092 * or
93 * ((cs == anything) and (ip == 0)).
94 *
Aaron Durbinf8468d42016-03-02 14:47:37 -060095 * The restrictions in reset16.inc mean that _start16bit initially
Eric Biedermanfcd5ace2004-10-14 19:29:29 +000096 * must be loaded at or above 0xffff0000 or below 0x100000.
97 *
Vikram Narayanan15370ca2012-01-21 20:19:14 +053098 * The linker scripts computes gdtptr16_offset by simply returning
Elyes HAOUASece26962018-08-07 12:24:16 +020099 * the low 16 bits. This means that the initial segment used
Eric Biedermanfcd5ace2004-10-14 19:29:29 +0000100 * when start is called must be 64K aligned. This should not
101 * restrict the address as the ip address can be anything.
Kyösti Mälkki78630152012-03-05 09:25:12 +0200102 *
103 * Also load an IDT with NULL limit to prevent the 16bit IDT being used
104 * in protected mode before c_start.S sets up a 32bit IDT when entering
Elyes HAOUAS585d1a02016-07-28 19:15:34 +0200105 * RAM stage. In practise: CPU will shutdown on any exception.
Kyösti Mälkki78630152012-03-05 09:25:12 +0200106 * See IA32 manual Vol 3A 19.26 Interrupts.
Eric Biedermanfcd5ace2004-10-14 19:29:29 +0000107 */
108
109 movw %cs, %ax
110 shlw $4, %ax
Kyösti Mälkki78630152012-03-05 09:25:12 +0200111 movw $nullidt_offset, %bx
112 subw %ax, %bx
113 lidt %cs:(%bx)
Eric Biedermanfcd5ace2004-10-14 19:29:29 +0000114 movw $gdtptr16_offset, %bx
115 subw %ax, %bx
Patrick Georgi938ef9f2014-01-18 16:24:24 +0100116 lgdtl %cs:(%bx)
Eric Biedermanfcd5ace2004-10-14 19:29:29 +0000117
118 movl %cr0, %eax
119 andl $0x7FFAFFD1, %eax /* PG,AM,WP,NE,TS,EM,MP = 0 */
120 orl $0x60000001, %eax /* CD, NW, PE = 1 */
121 movl %eax, %cr0
122
123 /* Restore BIST to %eax */
124 movl %ebp, %eax
125
126 /* Now that we are in protected mode jump to a 32 bit code segment. */
Patrick Georgi938ef9f2014-01-18 16:24:24 +0100127 ljmpl $ROM_CODE_SEG, $__protected_start
Eric Biedermanfcd5ace2004-10-14 19:29:29 +0000128
Li-Ta Lof84926e2004-11-04 18:36:06 +0000129 /**
130 * The gdt is defined in entry32.inc, it has a 4 Gb code segment
131 * at 0x08, and a 4 GB data segment at 0x10;
132 */
Eric Biedermanfcd5ace2004-10-14 19:29:29 +0000133.align 4
134.globl gdtptr16
135gdtptr16:
136 .word gdt_end - gdt -1 /* compute the table limit */
137 .long gdt /* we know the offset */
138
Stefan Reinauer71496be2011-06-01 14:01:46 -0700139.align 4
140.globl nullidt
141nullidt:
142 .word 0 /* limit */
143 .long 0
144 .word 0
145
Aaron Durbinf8468d42016-03-02 14:47:37 -0600146.globl _estart16bit
147_estart16bit:
Eric Biedermanfcd5ace2004-10-14 19:29:29 +0000148 .code32