Skip to content
This repository
  • 2 commits
  • 5 files changed
  • 0 comments
  • 1 contributor
39 lrt/bare/arch/amd64/Makefile
... ... @@ -1,22 +1,22 @@
1   -# Copyright (C) 2011 by Project SESA, Boston University
  1 +# Copyright (C) 2011 by Project SESA, Boston University
  2 +#
  3 +# Permission is hereby granted, free of charge, to any person obtaining a copy
  4 +# of this software and associated documentation files (the "Software"), to deal
  5 +# in the Software without restriction, including without limitation the rights
  6 +# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  7 +# copies of the Software, and to permit persons to whom the Software is
  8 +# furnished to do so, subject to the following conditions:
  9 +#
  10 +# The above copyright notice and this permission notice shall be included in
  11 +# all copies or substantial portions of the Software.
2 12 #
3   -# Permission is hereby granted, free of charge, to any person obtaining a copy
4   -# of this software and associated documentation files (the "Software"), to deal
5   -# in the Software without restriction, including without limitation the rights
6   -# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
7   -# copies of the Software, and to permit persons to whom the Software is
8   -# furnished to do so, subject to the following conditions:
9   -#
10   -# The above copyright notice and this permission notice shall be included in
11   -# all copies or substantial portions of the Software.
12   -#
13   -# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
14   -# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
15   -# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
16   -# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
17   -# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
18   -# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
19   -# THE SOFTWARE.
  13 +# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  14 +# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  15 +# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  16 +# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  17 +# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  18 +# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  19 +# THE SOFTWARE.
20 20
21 21 AS_32=as --32
22 22 CC_32=cc -m32
@@ -36,7 +36,8 @@ OFILES=\
36 36 start.o\
37 37 serial.o\
38 38 lrt_start.o\
39   - fmt.o
  39 + fmt.o\
  40 + premalloc.o
40 41
41 42 $(TARG): $(OFILES)
42 43 $(LD_32) -o $@ $(LDFLAGS_32) $(OFILES)
7 lrt/bare/arch/amd64/lrt_start.c
@@ -23,6 +23,7 @@
23 23 #include <stdint.h>
24 24 #include "serial.h"
25 25 #include "fmt.h"
  26 +#include "premalloc.h"
26 27
27 28 struct mboot_args_t {
28 29 uint32_t flags;
@@ -49,9 +50,13 @@ struct mboot_args_t {
49 50 };
50 51
51 52 void main(struct mboot_args_t *args) {
  53 + void *endofk, *aligned;
52 54 lrt_serial_init(COM1);
53 55 stdout.buf = COM1;
54 56 stdout.putch = lrt_serial_putch;
55   - printf("Hello, SESA!");
  57 + printf("Hello, SESA!\n");
  58 + endofk = premalloc(7,1);
  59 + aligned = premalloc(3, 4);
  60 + printf("endofk = 0x%x, aligned = 0x%x\n", endofk, aligned);
56 61 }
57 62
56 lrt/bare/arch/amd64/paging.c
... ... @@ -0,0 +1,56 @@
  1 +/*
  2 + * Copyright (C) 2011 by Project SESA, Boston University
  3 + *
  4 + * Permission is hereby granted, free of charge, to any person obtaining a copy
  5 + * of this software and associated documentation files (the "Software"), to deal
  6 + * in the Software without restriction, including without limitation the rights
  7 + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  8 + * copies of the Software, and to permit persons to whom the Software is
  9 + * furnished to do so, subject to the following conditions:
  10 + *
  11 + * The above copyright notice and this permission notice shall be included in
  12 + * all copies or substantial portions of the Software.
  13 + *
  14 + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  15 + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  16 + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  17 + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  18 + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  19 + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  20 + * THE SOFTWARE.
  21 + */
  22 +
  23 +static const uint32_t CR0_PG = (1<<31);
  24 +static const uint32_t CR4_PAE = (1<<5);
  25 +static const uint32_t IA_32E_LME = (1<<8);
  26 +
  27 +static const uint32_t IA32_EFER = 0xc0000080;
  28 +
  29 +static inline void load_pml4(pml4_t *pml4) {
  30 + __asm__ volatile ("movl %0, %%cr3" :: "r"(pml4) :);
  31 +}
  32 +
  33 +static inline void enable_pae(void) {
  34 + uint32_t cr4;
  35 + __asm__ volatile ("movl %%cr4, %0" : "=r"(cr4) ::);
  36 + cr4 |= CR4_PAE;
  37 + __asm__ volatile ("movl %0, %%cr4" :: "r"(cr4) :);
  38 +}
  39 +
  40 +static inline void enable_longmode(void) {
  41 + __asm__ volatile("movl %%ecx, %0\n"
  42 + "rdmsr\n"
  43 + "orl %%eax, %1\n"
  44 + "wrmsr\n"
  45 + :: "r"(IA32_EFER), "r"(IA_32E_LME)
  46 + : "eax", "ecx", "edx");
  47 +}
  48 +
  49 +static inline void enable_paging(void){
  50 + uint32_t cr0;
  51 + __asm__ volatile ("movl %%cr0, %0" : "=r"(cr0) ::);
  52 + cr0 |= CR0_PG;
  53 + __asm__ volatile ("movl %0, %%cr0" :: "r"(cr0) :);
  54 +}
  55 +
  56 +
16 lrt/bare/arch/amd64/premalloc.c
... ... @@ -0,0 +1,16 @@
  1 +#include <stdint.h>
  2 +#include "premalloc.h"
  3 +
  4 +extern uintptr_t kend; /* in link.ld. located just after the kernel image. */
  5 +static uintptr_t *nextptr = &kend;
  6 +
  7 +void *premalloc(uintptr_t size, uintptr_t align) {
  8 + uintptr_t skew; /* how far past the last aligned address we are */
  9 + void *ret;
  10 + skew = (uintptr_t)nextptr % align;
  11 + if(skew)
  12 + nextptr = (uintptr_t*)((align - skew) + (uintptr_t)nextptr);
  13 + ret = nextptr;
  14 + nextptr = (uintptr_t*)((uintptr_t)nextptr + size);
  15 + return ret;
  16 +}
34 lrt/bare/arch/amd64/premalloc.h
... ... @@ -0,0 +1,34 @@
  1 +#ifndef __AMD64_PREMALLOC_H__
  2 +#define __AMD64_PREMALLOC_H__
  3 +
  4 +/*
  5 + * Copyright (C) 2011 by Project SESA, Boston University
  6 + *
  7 + * Permission is hereby granted, free of charge, to any person obtaining a copy
  8 + * of this software and associated documentation files (the "Software"), to deal
  9 + * in the Software without restriction, including without limitation the rights
  10 + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  11 + * copies of the Software, and to permit persons to whom the Software is
  12 + * furnished to do so, subject to the following conditions:
  13 + *
  14 + * The above copyright notice and this permission notice shall be included in
  15 + * all copies or substantial portions of the Software.
  16 + *
  17 + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  18 + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  19 + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  20 + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  21 + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  22 + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  23 + * THE SOFTWARE.
  24 + */
  25 +
  26 +#include <stdint.h>
  27 +
  28 +/* Allocates a chunk of memory of size `size` aligned on an `align`-byte boundary.
  29 + This memory cannot be freed.
  30 +
  31 + Memory allocation starts from the end of the kernel binary. */
  32 +void *premalloc(uintptr_t size, uintptr_t align);
  33 +
  34 +#endif

No commit comments for this range

Something went wrong with that request. Please try again.