blob: 1ecd6ed422d5fab390a102143507dae7ae0946d9 [file] [log] [blame]
Kyösti Mälkki03083132020-11-22 00:34:13 +02001/* SPDX-License-Identifier: BSD-3-Clause */
2
Eric Biedermanfcd5ace2004-10-14 19:29:29 +00003/*
Kyösti Mälkki0dbfb542011-11-22 20:21:06 +02004 * This software and ancillary information (herein called SOFTWARE)
5 * called LinuxBIOS is made available under the terms described here.
6 *
7 * The SOFTWARE has been approved for release with associated
8 * LA-CC Number 00-34. Unless otherwise indicated, this SOFTWARE has
9 * been authored by an employee or employees of the University of
10 * California, operator of the Los Alamos National Laboratory under
11 * Contract No. W-7405-ENG-36 with the U.S. Department of Energy.
12 *
13 * The U.S. Government has rights to use, reproduce, and distribute this
14 * SOFTWARE. The public may copy, distribute, prepare derivative works
15 * and publicly display this SOFTWARE without charge, provided that this
16 * Notice and any statement of authorship are reproduced on all copies.
17 *
18 * Neither the Government nor the University makes any warranty, express
19 * or implied, or assumes any liability or responsibility for the use of
20 * this SOFTWARE. If SOFTWARE is modified to produce derivative works,
21 * such modified SOFTWARE should be clearly marked, so as not to confuse
22 * it with the version available from LANL.
23 *
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älkkidf771c12019-12-21 10:17:56 +020031#include <cpu/x86/post_code.h>
Kyösti Mälkki34856572019-01-09 20:30:52 +020032
Kyösti Mälkki03083132020-11-22 00:34:13 +020033.section .init._start, "ax", @progbits
34
35/* Symbol _start16bit must reachable from the reset vector, and be aligned to
36 * 4kB to start AP CPUs with Startup IPI message without RAM.
Kyösti Mälkki34856572019-01-09 20:30:52 +020037 */
38.align 4096
Eric Biedermanfcd5ace2004-10-14 19:29:29 +000039.code16
Aaron Durbinf8468d42016-03-02 14:47:37 -060040.globl _start16bit
41.type _start16bit, @function
Eric Biedermanfcd5ace2004-10-14 19:29:29 +000042
Aaron Durbinf8468d42016-03-02 14:47:37 -060043_start16bit:
Eric Biedermanfcd5ace2004-10-14 19:29:29 +000044 cli
45 /* Save the BIST result */
46 movl %eax, %ebp
Julius Wernercd49cce2019-03-05 16:53:33 -080047#if !CONFIG(NO_EARLY_BOOTBLOCK_POSTCODES)
Kyösti Mälkki2a40ebc2011-11-21 08:16:20 +020048 post_code(POST_RESET_VECTOR_CORRECT)
Martin Roth14554372015-11-12 14:02:42 -070049#endif
Kyösti Mälkki2a40ebc2011-11-21 08:16:20 +020050
Kyösti Mälkki0dbfb542011-11-22 20:21:06 +020051 /* IMMEDIATELY invalidate the translation lookaside buffer (TLB) before
52 * executing any further code. Even though paging is disabled we
53 * could still get false address translations due to the TLB if we
54 * didn't invalidate it. Thanks to kmliu@sis.com.tw for this TLB fix.
55 */
56
Eric Biedermanfcd5ace2004-10-14 19:29:29 +000057 xorl %eax, %eax
58 movl %eax, %cr3 /* Invalidate TLB*/
59
Eric Biedermanfcd5ace2004-10-14 19:29:29 +000060 /* Invalidating the cache here seems to be a bad idea on
61 * modern processors. Don't.
62 * If we are hyperthreaded or we have multiple cores it is bad,
63 * for SMP startup. On Opterons it causes a 5 second delay.
64 * Invalidating the cache was pure paranoia in any event.
Paul Menzel39851122018-02-14 15:13:38 +010065 * If your CPU needs it you can write a CPU dependent version of
Eric Biedermanfcd5ace2004-10-14 19:29:29 +000066 * entry16.inc.
67 */
68
69 /* Note: gas handles memory addresses in 16 bit code very poorly.
70 * In particular it doesn't appear to have a directive allowing you
71 * associate a section or even an absolute offset with a segment register.
72 *
73 * This means that anything except cs:ip relative offsets are
74 * a real pain in 16 bit mode. And explains why it is almost
Vikram Narayanan15370ca2012-01-21 20:19:14 +053075 * impossible to get gas to do lgdt correctly.
Eric Biedermanfcd5ace2004-10-14 19:29:29 +000076 *
77 * One way to work around this is to have the linker do the
78 * math instead of the assembler. This solves the very
Raul E Rangelfa52f312020-04-24 13:57:26 -060079 * practical problem of being able to write code that can
Eric Biedermanfcd5ace2004-10-14 19:29:29 +000080 * be relocated.
81 *
Stefan Reinauer14e22772010-04-27 06:56:47 +000082 * An lgdt call before we have memory enabled cannot be
Eric Biedermanfcd5ace2004-10-14 19:29:29 +000083 * position independent, as we cannot execute a call
84 * instruction to get our current instruction pointer.
Raul E Rangelfa52f312020-04-24 13:57:26 -060085 * So while this code is relocatable it isn't arbitrarily
Eric Biedermanfcd5ace2004-10-14 19:29:29 +000086 * relocatable.
87 *
Stefan Reinauer14e22772010-04-27 06:56:47 +000088 * The criteria for relocation have been relaxed to their
Eric Biedermanfcd5ace2004-10-14 19:29:29 +000089 * utmost, so that we can use the same code for both
Elyes HAOUASd6e96862016-08-21 10:12:15 +020090 * our initial entry point and startup of the second CPU.
Aaron Durbinf8468d42016-03-02 14:47:37 -060091 * The code assumes when executing at _start16bit that:
92 * (((cs & 0xfff) == 0) and (ip == _start16bit & 0xffff))
Eric Biedermanfcd5ace2004-10-14 19:29:29 +000093 * or
94 * ((cs == anything) and (ip == 0)).
95 *
Aaron Durbinf8468d42016-03-02 14:47:37 -060096 * The restrictions in reset16.inc mean that _start16bit initially
Eric Biedermanfcd5ace2004-10-14 19:29:29 +000097 * must be loaded at or above 0xffff0000 or below 0x100000.
98 *
Vikram Narayanan15370ca2012-01-21 20:19:14 +053099 * The linker scripts computes gdtptr16_offset by simply returning
Elyes HAOUASece26962018-08-07 12:24:16 +0200100 * the low 16 bits. This means that the initial segment used
Eric Biedermanfcd5ace2004-10-14 19:29:29 +0000101 * when start is called must be 64K aligned. This should not
102 * restrict the address as the ip address can be anything.
Kyösti Mälkki78630152012-03-05 09:25:12 +0200103 *
104 * Also load an IDT with NULL limit to prevent the 16bit IDT being used
105 * in protected mode before c_start.S sets up a 32bit IDT when entering
Elyes HAOUAS585d1a02016-07-28 19:15:34 +0200106 * RAM stage. In practise: CPU will shutdown on any exception.
Kyösti Mälkki78630152012-03-05 09:25:12 +0200107 * See IA32 manual Vol 3A 19.26 Interrupts.
Eric Biedermanfcd5ace2004-10-14 19:29:29 +0000108 */
109
110 movw %cs, %ax
111 shlw $4, %ax
Kyösti Mälkki78630152012-03-05 09:25:12 +0200112 movw $nullidt_offset, %bx
113 subw %ax, %bx
114 lidt %cs:(%bx)
Kyösti Mälkkidc873cc2020-11-21 17:59:41 +0200115 movw $gdtptr_offset, %bx
Eric Biedermanfcd5ace2004-10-14 19:29:29 +0000116 subw %ax, %bx
Patrick Georgi938ef9f2014-01-18 16:24:24 +0100117 lgdtl %cs:(%bx)
Eric Biedermanfcd5ace2004-10-14 19:29:29 +0000118
119 movl %cr0, %eax
120 andl $0x7FFAFFD1, %eax /* PG,AM,WP,NE,TS,EM,MP = 0 */
121 orl $0x60000001, %eax /* CD, NW, PE = 1 */
122 movl %eax, %cr0
123
124 /* Restore BIST to %eax */
125 movl %ebp, %eax
126
127 /* Now that we are in protected mode jump to a 32 bit code segment. */
Patrick Georgi938ef9f2014-01-18 16:24:24 +0100128 ljmpl $ROM_CODE_SEG, $__protected_start
Eric Biedermanfcd5ace2004-10-14 19:29:29 +0000129
Li-Ta Lof84926e2004-11-04 18:36:06 +0000130 /**
Kyösti Mälkki97b76f72020-11-19 16:41:28 +0200131 * The gdt is defined in gdt_init.S, it has a 4 Gb code segment
Li-Ta Lof84926e2004-11-04 18:36:06 +0000132 * at 0x08, and a 4 GB data segment at 0x10;
133 */
Kyösti Mälkkidc873cc2020-11-21 17:59:41 +0200134__gdtptr:
135 .long gdtptr
Eric Biedermanfcd5ace2004-10-14 19:29:29 +0000136
Stefan Reinauer71496be2011-06-01 14:01:46 -0700137.align 4
138.globl nullidt
139nullidt:
140 .word 0 /* limit */
141 .long 0
142 .word 0