summaryrefslogtreecommitdiff
path: root/arch/x86/machine/boot.c
blob: 6934896e707d7105df9168c77a0286482600c506 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
/*
 * Copyright (c) 2010-2017 Richard Braun.
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 *
 *
 * Early initialization procedure for x86.
 *
 * This module is separated in assembly and C code. The former is where
 * the first instructions are run, and where actions that aren't possible,
 * easy or clean in C are performed.
 *
 * When the boot loader passes control to the kernel, the main processor is
 * in protected mode, paging is disabled, and some boot data are availabe
 * outside the kernel. This module first sets up a basic physical memory
 * allocator so that it can allocate page tables without corrupting the
 * boot data. The .boot section is linked at physical addresses, so that
 * it can run with and without paging enabled. The page tables must properly
 * configure an identity mapping so that this remains true as long as
 * initialization code and data are used. Once the VM system is available,
 * boot data are copied in kernel allocated buffers and their original pages
 * are freed.
 *
 * On amd64, 64-bit code cannot run in legacy or compatibility mode. In order
 * to walk the boot data structures, the kernel must either run 32-bit code
 * (e.g. converting ELF32 to ELF64 objects before linking them) or establish
 * a temporary identity mapping for the first 4 GiB of physical memory. As a
 * way to simplify development, and make it possible to use 64-bit code
 * almost everywhere, the latter solution is implemented (a small part of
 * 32-bit code is required until the identity mapping is in place). Mentions
 * to "enabling paging" do not refer to this initial identity mapping.
 *
 * TODO EFI support.
 */

#include <stdalign.h>
#include <stddef.h>
#include <stdint.h>
#include <string.h>

#include <kern/arg.h>
#include <kern/init.h>
#include <kern/kmem.h>
#include <kern/kernel.h>
#include <kern/log.h>
#include <kern/macros.h>
#include <kern/panic.h>
#include <kern/thread.h>
#include <machine/acpi.h>
#include <machine/atcons.h>
#include <machine/biosmem.h>
#include <machine/boot.h>
#include <machine/cga.h>
#include <machine/cpu.h>
#include <machine/elf.h>
#include <machine/multiboot.h>
#include <machine/page.h>
#include <machine/pmap.h>
#include <machine/strace.h>
#include <machine/uart.h>
#include <vm/vm_kmem.h>

alignas(CPU_DATA_ALIGN) char boot_stack[BOOT_STACK_SIZE] __bootdata;
alignas(CPU_DATA_ALIGN) char boot_ap_stack[BOOT_STACK_SIZE] __bootdata;
unsigned int boot_ap_id __bootdata;

#ifdef __LP64__
alignas(PAGE_SIZE) pmap_pte_t boot_pml4[PMAP_L3_PTES_PER_PT] __bootdata;
alignas(PAGE_SIZE) pmap_pte_t boot_pdpt[PMAP_L2_PTES_PER_PT] __bootdata;
alignas(PAGE_SIZE) pmap_pte_t boot_pdir[4 * PMAP_L1_PTES_PER_PT] __bootdata;
char boot_panic_long_mode_msg[] __bootdata
    = "boot: processor doesn't support long mode";
#endif /* __LP64__ */

struct cpu_tls_seg boot_tls_seg __bootdata = {
    .ssp_guard_word = SSP_GUARD_WORD,
};

/*
 * This TLS segment descriptor is copied early at boot time into the
 * temporary boot GDT. However, it is incomplete. Assembly code
 * completes it before writing it into the boot GDT before calling
 * any C function, since those may require the TLS segment ready if
 * stack protection is enabled.
 */
struct cpu_seg_desc boot_tls_seg_desc __bootdata = {
    .low = (sizeof(boot_tls_seg) & 0xffff),

    .high = CPU_DESC_DB
            | ((sizeof(boot_tls_seg) >> 16) & 0xf)
            | CPU_DESC_PRESENT
            | CPU_DESC_S
            | CPU_DESC_TYPE_DATA,
};

/*
 * Copies of the multiboot data passed by the boot loader.
 */
static struct multiboot_raw_info boot_raw_mbi __bootdata;
static struct multiboot_info boot_mbi __initdata;

static char boot_tmp_cmdline[ARG_CMDLINE_MAX_SIZE] __bootdata;

static char boot_panic_intro_msg[] __bootdata = "panic: ";
static char boot_panic_loader_msg[] __bootdata
    = "boot: not started by a multiboot compliant boot loader";
static char boot_panic_meminfo_msg[] __bootdata
    = "boot: missing basic memory information";
static char boot_panic_cmdline_msg[] __bootdata
    = "boot: command line too long";

void * __boot
boot_memcpy(void *dest, const void *src, size_t n)
{
    const char *src_ptr;
    char *dest_ptr;
    size_t i;

    dest_ptr = dest;
    src_ptr = src;

    for (i = 0; i < n; i++) {
        *dest_ptr = *src_ptr;
        dest_ptr++;
        src_ptr++;
    }

    return dest;
}

void * __boot
boot_memmove(void *dest, const void *src, size_t n)
{
    const char *src_ptr;
    char *dest_ptr;
    size_t i;

    if (dest <= src) {
        dest_ptr = dest;
        src_ptr = src;

        for (i = 0; i < n; i++) {
            *dest_ptr = *src_ptr;
            dest_ptr++;
            src_ptr++;
        }
    } else {
        dest_ptr = dest + n - 1;
        src_ptr = src + n - 1;

        for (i = 0; i < n; i++) {
            *dest_ptr = *src_ptr;
            dest_ptr--;
            src_ptr--;
        }
    }

    return dest;
}

void * __boot
boot_memset(void *s, int c, size_t n)
{
    char *buffer;
    size_t i;

    buffer = s;

    for (i = 0; i < n; i++) {
        buffer[i] = c;
    }

    return s;
}

size_t __boot
boot_strlen(const char *s)
{
    const char *start;

    start = s;

    while (*s != '\0') {
        s++;
    }

    return (s - start);
}

void __boot
boot_panic(const char *msg)
{
    uint16_t *ptr, *end;
    const char *s;

    ptr = (uint16_t *)BOOT_CGAMEM;
    end = ptr + BOOT_CGACHARS;

    s = boot_panic_intro_msg;

    while ((ptr < end) && (*s != '\0')) {
        *ptr = (BOOT_CGACOLOR << 8) | *s;
        ptr++;
        s++;
    }

    s = msg;

    while ((ptr < end) && (*s != '\0')) {
        *ptr = (BOOT_CGACOLOR << 8) | *s;
        ptr++;
        s++;
    }

    while (ptr < end) {
        *ptr = (BOOT_CGACOLOR << 8) | ' ';
        ptr++;
    }

    cpu_halt();

    /* Never reached */
}

static void __boot
boot_save_mod_cmdline_sizes(struct multiboot_raw_info *mbi)
{
    struct multiboot_raw_module *mod;
    uint32_t i;

    if (mbi->flags & MULTIBOOT_LOADER_MODULES) {
        uintptr_t addr;

        addr = mbi->mods_addr;

        for (i = 0; i < mbi->mods_count; i++) {
            mod = (struct multiboot_raw_module *)addr + i;
            mod->reserved = boot_strlen((char *)(uintptr_t)mod->string) + 1;
        }
    }
}

static void __boot
boot_register_data(const struct multiboot_raw_info *mbi)
{
    struct multiboot_raw_module *mod;
    struct elf_shdr *shdr;
    uintptr_t tmp;
    unsigned int i;

    biosmem_register_boot_data((uintptr_t)&_boot,
                               BOOT_VTOP((uintptr_t)&_end), false);

    if (mbi->flags & MULTIBOOT_LOADER_MODULES) {
        i = mbi->mods_count * sizeof(struct multiboot_raw_module);
        biosmem_register_boot_data(mbi->mods_addr, mbi->mods_addr + i, true);

        tmp = mbi->mods_addr;

        for (i = 0; i < mbi->mods_count; i++) {
            mod = (struct multiboot_raw_module *)tmp + i;
            biosmem_register_boot_data(mod->mod_start, mod->mod_end, true);

            if (mod->string != 0) {
                biosmem_register_boot_data(mod->string,
                                           mod->string + mod->reserved, true);
            }
        }
    }

    if (mbi->flags & MULTIBOOT_LOADER_SHDR) {
        tmp = mbi->shdr_num * mbi->shdr_size;
        biosmem_register_boot_data(mbi->shdr_addr, mbi->shdr_addr + tmp, true);

        tmp = mbi->shdr_addr;

        for (i = 0; i < mbi->shdr_num; i++) {
            shdr = (struct elf_shdr *)(tmp + (i * mbi->shdr_size));

            if ((shdr->type != ELF_SHT_SYMTAB)
                && (shdr->type != ELF_SHT_STRTAB)) {
                continue;
            }

            biosmem_register_boot_data(shdr->addr, shdr->addr + shdr->size, true);
        }
    }
}

pmap_pte_t * __boot
boot_setup_paging(struct multiboot_raw_info *mbi, unsigned long eax)
{
    if (eax != MULTIBOOT_LOADER_MAGIC) {
        boot_panic(boot_panic_loader_msg);
    }

    if (!(mbi->flags & MULTIBOOT_LOADER_MEMORY)) {
        boot_panic(boot_panic_meminfo_msg);
    }

    /*
     * Save the multiboot data passed by the boot loader, initialize the
     * bootstrap allocator and set up paging.
     */
    boot_memmove(&boot_raw_mbi, mbi, sizeof(boot_raw_mbi));

    /*
     * The kernel command line must be passed as early as possible to the
     * arg module so that other modules can look up options. Instead of
     * mapping it later, make a temporary copy.
     */
    if (!(mbi->flags & MULTIBOOT_LOADER_CMDLINE)) {
        boot_tmp_cmdline[0] = '\0';
    } else {
        uintptr_t addr;
        size_t length;

        addr = mbi->cmdline;
        length = boot_strlen((const char *)addr) + 1;

        if (length > ARRAY_SIZE(boot_tmp_cmdline)) {
            boot_panic(boot_panic_cmdline_msg);
        }

        boot_memcpy(boot_tmp_cmdline, (const char *)addr, length);
    }

    if ((mbi->flags & MULTIBOOT_LOADER_MODULES) && (mbi->mods_count == 0)) {
        boot_raw_mbi.flags &= ~MULTIBOOT_LOADER_MODULES;
    }

    /*
     * The module command lines will be memory mapped later during
     * initialization. Their respective sizes must be saved.
     */
    boot_save_mod_cmdline_sizes(&boot_raw_mbi);
    boot_register_data(&boot_raw_mbi);
    biosmem_bootstrap(&boot_raw_mbi);
    return pmap_setup_paging();
}

#ifdef CONFIG_X86_PAE
#define BOOT_PAE_LABEL " PAE"
#else /* CONFIG_X86_PAE */
#define BOOT_PAE_LABEL
#endif /* CONFIG_X86_PAE */

void __init
boot_log_info(void)
{
    log_info(KERNEL_NAME "/" CONFIG_SUBARCH " " KERNEL_VERSION
             BOOT_PAE_LABEL);
}

static void * __init
boot_save_memory(uint32_t addr, size_t size)
{
    uintptr_t map_addr;
    size_t map_size;
    const void *src;
    void *copy;

    /*
     * Creates temporary virtual mappings because, on 32-bits systems,
     * there is no guarantee that the boot data will be available from
     * the direct physical mapping.
     */
    src = vm_kmem_map_pa(addr, size, &map_addr, &map_size);

    if (src == NULL) {
        panic("boot: unable to map boot data in kernel map");
    }

    copy = kmem_alloc(size);

    if (copy == NULL) {
        panic("boot: unable to allocate memory for boot data copy");
    }

    memcpy(copy, src, size);
    vm_kmem_unmap_pa(map_addr, map_size);
    return copy;
}

static void __init
boot_save_mod(struct multiboot_module *dest_mod,
              const struct multiboot_raw_module *src_mod)
{
    uintptr_t map_addr;
    size_t size, map_size;
    const void *src;
    void *copy;

    size = src_mod->mod_end - src_mod->mod_start;
    src = vm_kmem_map_pa(src_mod->mod_start, size, &map_addr, &map_size);

    if (src == NULL) {
        panic("boot: unable to map module in kernel map");
    }

    copy = kmem_alloc(size);

    if (copy == NULL) {
        panic("boot: unable to allocate memory for module copy");
    }

    memcpy(copy, src, size);
    vm_kmem_unmap_pa(map_addr, map_size);

    dest_mod->mod_start = copy;
    dest_mod->mod_end = copy + size;

    if (src_mod->string == 0) {
        dest_mod->string = NULL;
    } else {
        dest_mod->string = boot_save_memory(src_mod->string, src_mod->reserved);
    }
}

static void __init
boot_save_mods(void)
{
    const struct multiboot_raw_module *src;
    struct multiboot_module *dest;
    uintptr_t map_addr;
    size_t size, map_size;
    uint32_t i;

    if (!(boot_raw_mbi.flags & MULTIBOOT_LOADER_MODULES)) {
        boot_mbi.mods_addr = NULL;
        boot_mbi.mods_count = boot_raw_mbi.mods_count;
        return;
    }

    size = boot_raw_mbi.mods_count * sizeof(struct multiboot_raw_module);
    src = vm_kmem_map_pa(boot_raw_mbi.mods_addr, size, &map_addr, &map_size);

    if (src == NULL) {
        panic("boot: unable to map module table in kernel map");
    }

    size = boot_raw_mbi.mods_count * sizeof(struct multiboot_module);
    dest = kmem_alloc(size);

    if (dest == NULL) {
        panic("boot: unable to allocate memory for the module table");
    }

    for (i = 0; i < boot_raw_mbi.mods_count; i++) {
        boot_save_mod(&dest[i], &src[i]);
    }

    vm_kmem_unmap_pa(map_addr, map_size);

    boot_mbi.mods_addr = dest;
    boot_mbi.mods_count = boot_raw_mbi.mods_count;
}

/*
 * Copy boot data in kernel allocated memory.
 *
 * At this point, the only required boot data are the modules and the command
 * line strings. Optionally, the kernel can use the symbol table, if passed by
 * the boot loader. Once the boot data are managed as kernel buffers, their
 * backing pages can be freed.
 */
static int __init
boot_save_data(void)
{
    boot_mbi.flags = boot_raw_mbi.flags;
    boot_save_mods();
    return 0;
}

INIT_OP_DEFINE(boot_save_data,
               INIT_OP_DEP(kmem_setup, true),
               INIT_OP_DEP(panic_setup, true),
               INIT_OP_DEP(vm_kmem_setup, true));

void __init
boot_main(void)
{
    arg_set_cmdline(boot_tmp_cmdline);
    strace_set_mbi(&boot_raw_mbi);
    kernel_main();

    /* Never reached */
}

void __init
boot_ap_main(void)
{
    cpu_ap_setup();
    thread_ap_setup();
    pmap_ap_setup();
    kernel_ap_main();

    /* Never reached */
}

/*
 * Init operation aliases.
 */

static int __init
boot_bootstrap_console(void)
{
    return 0;
}

INIT_OP_DEFINE(boot_bootstrap_console,
               INIT_OP_DEP(atcons_bootstrap, true),
               INIT_OP_DEP(uart_bootstrap, true));

static int __init
boot_setup_console(void)
{
    return 0;
}

INIT_OP_DEFINE(boot_setup_console,
               INIT_OP_DEP(atcons_setup, true),
               INIT_OP_DEP(uart_setup, true));

static int __init
boot_load_vm_page_zones(void)
{
    return 0;
}

INIT_OP_DEFINE(boot_load_vm_page_zones,
               INIT_OP_DEP(biosmem_setup, true));

static int __init
boot_setup_intr(void)
{
    return 0;
}

INIT_OP_DEFINE(boot_setup_intr,
               INIT_OP_DEP(acpi_setup, true));

static int __init
boot_setup_shutdown(void)
{
    return 0;
}

INIT_OP_DEFINE(boot_setup_shutdown,
               INIT_OP_DEP(acpi_setup, true),
               INIT_OP_DEP(cpu_setup_shutdown, true));