Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 9 additions & 11 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ AS = $(TOOLPREFIX)gas
LD = $(TOOLPREFIX)ld
OBJCOPY = $(TOOLPREFIX)objcopy
OBJDUMP = $(TOOLPREFIX)objdump
CFLAGS = -fno-pic -static -fno-builtin -fno-strict-aliasing -Wall -MD -g -ggdb -fno-omit-frame-pointer
CFLAGS = -fno-pic -static -fno-builtin -fno-strict-aliasing -Wall -MD -g -ggdb -fno-omit-frame-pointer -ffunction-sections
CFLAGS += -ffreestanding -fno-common -nostdlib -Iinclude -gdwarf-2 $(XFLAGS) $(OPT)
CFLAGS += $(shell $(CC) -fno-stack-protector -E -x c /dev/null >/dev/null 2>&1 && echo -fno-stack-protector)
ASFLAGS = -fno-pic -gdwarf-2 -Wa,-divide -Iinclude $(XFLAGS)
Expand Down Expand Up @@ -167,21 +167,21 @@ ULIB = uobj/ulib.o uobj/usys.o uobj/printf.o uobj/umalloc.o uobj/string.o

fs/bin/%: uobj/%.o $(ULIB)
@mkdir -p fs out fs/bin
$(LD) $(LDFLAGS) -N -e main -Ttext 0 -o $@ $^
$(LD) $(LDFLAGS) --gc-sections -N -e main -Ttext 0 -o $@ $^
$(OBJDUMP) -S $@ > out/$*.asm
$(OBJDUMP) -t $@ | sed '1,/SYMBOL TABLE/d; s/ .* / /; /^$$/d' > bin/$*.sym

fs/%: uobj/%.o $(ULIB)
@mkdir -p fs out fs/bin
$(LD) $(LDFLAGS) -N -e main -Ttext 0 -o $@ $^
$(LD) $(LDFLAGS) --gc-sections -N -e main -Ttext 0 -o $@ $^
$(OBJDUMP) -S $@ > out/$*.asm
$(OBJDUMP) -t $@ | sed '1,/SYMBOL TABLE/d; s/ .* / /; /^$$/d' > bin/$*.sym

fs/forktest: uobj/forktest.o $(ULIB)
@mkdir -p fs
# forktest has less library code linked in - needs to be small
# in order to be able to max out the proc table.
$(LD) $(LDFLAGS) -N -e main -Ttext 0 -o fs/forktest uobj/forktest.o uobj/ulib.o uobj/usys.o
$(LD) $(LDFLAGS) --gc-sections -N -e main -Ttext 0 -o fs/forktest uobj/forktest.o uobj/ulib.o uobj/usys.o
$(OBJDUMP) -S fs/forktest > out/forktest.asm

out/mkfs: tools/mkfs.c include/vfs.h
Expand Down Expand Up @@ -236,12 +236,10 @@ fs.img: out/mkfs fs/LICENSE $(UPROGS) $(SUBPROGS)

dd if=/dev/zero of=bin/fs-ext2.img bs=1k count=2000
mkfs -t ext2 -i 1024 -b 1024 -F bin/fs-ext2.img
mkdir /tmp/loop
mount -o loop bin/fs-ext2.img /tmp/loop
cp -r ./fs/ /tmp/loop/
umount /tmp/loop


mkdir -p /tmp/loop
sudo mount -o loop bin/fs-ext2.img /tmp/loop
sudo cp -r ./fs/ /tmp/loop/
sudo umount /tmp/loop
-include */*.d

clean:
Expand All @@ -259,7 +257,7 @@ binaries : fs.img boot.img
cp -r fs ./bin/
cd ./bin/ && tar -xvzf Xv64.vmwarevm.tar.gz && rm Xv64.vmwarevm.tar.gz && cd ..
qemu-img convert boot.img -O vmdk bin/Xv64.vmwarevm/boot.vmdk
mkdir bin/artifacts
mkdir -p bin/artifacts
cp -r uobj bin/artifacts/
cp -r kobj bin/artifacts/
cp -r out bin/artifacts/
Expand Down
4 changes: 3 additions & 1 deletion include/defs.h
Original file line number Diff line number Diff line change
Expand Up @@ -78,8 +78,10 @@ int cpunum(void);
extern volatile uint* lapic;
void lapiceoi(void);
void lapicinit(void);
void lapicstartap(uchar, uint);
void microdelay(int);
void lapicstartap(uchar, uint);
void lapicx2enable(uint);
void lapicsendipi(uint, uint);

// log.c
void initlog(void);
Expand Down
17 changes: 2 additions & 15 deletions include/mmu.h
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
// This file contains definitions for the
// x86 memory management unit (MMU).

#include "x86_defs.h"

// Eflags register
#define FL_CF 0x00000001 // Carry Flag
#define FL_PF 0x00000004 // Parity Flag
Expand All @@ -24,21 +26,6 @@
#define FL_VIP 0x00100000 // Virtual Interrupt Pending
#define FL_ID 0x00200000 // ID flag

// Control Register flags
#define CR0_PE 0x00000001 // Protection Enable
#define CR0_MP 0x00000002 // Monitor coProcessor
#define CR0_EM 0x00000004 // Emulation
#define CR0_TS 0x00000008 // Task Switched
#define CR0_ET 0x00000010 // Extension Type
#define CR0_NE 0x00000020 // Numeric Errror
#define CR0_WP 0x00010000 // Write Protect
#define CR0_AM 0x00040000 // Alignment Mask
#define CR0_NW 0x20000000 // Not Writethrough
#define CR0_CD 0x40000000 // Cache Disable
#define CR0_PG 0x80000000 // Paging

#define CR4_PSE 0x00000010 // Page size extension

#define SEG_KCODE 1 // kernel code
#define SEG_KDATA 2 // kernel data+stack
#define SEG_KCPU 3 // kernel per-cpu data
Expand Down
4 changes: 4 additions & 0 deletions include/proc.h
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,10 @@ struct cpu {
// Cpu-local storage variables; see below
void *local;
struct proc *proc;

char name[50]; // cpuid name
char vendor[13]; // cpuid vendor
uint32 model; // cpuid model
};

extern struct cpu cpus[NCPU];
Expand Down
2 changes: 1 addition & 1 deletion include/types.h
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ typedef unsigned char uchar;
typedef unsigned char uint8;
typedef unsigned short uint16;
typedef unsigned int uint32;
typedef unsigned long uint64;
typedef unsigned long long uint64;

//explicit-length signed ints
typedef char int8;
Expand Down
4 changes: 2 additions & 2 deletions include/unix/stdint.h
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,10 @@
typedef char int8_t;
typedef short int16_t;
typedef int int32_t;
typedef long int64_t;
typedef long long int64_t;


typedef unsigned char uint8_t;
typedef unsigned short uint16_t;
typedef unsigned int uint32_t;
typedef unsigned long uint64_t;
typedef unsigned long long uint64_t;
38 changes: 37 additions & 1 deletion include/x86.h
Original file line number Diff line number Diff line change
Expand Up @@ -180,7 +180,18 @@ static inline unsigned int amd64_xchg(volatile unsigned int *addr, unsigned long
unsigned int result;

// The + in "+m" denotes a read-modify-write operand.
asm volatile ("lock; xchgl %0, %1" :
asm volatile ("lock xchgl %0, %1" :
"+m" (*addr), "=a" (result) :
"1" (newval) :
"cc");
return result;
}

static inline unsigned int amd64_xadd(volatile unsigned int *addr, unsigned long newval) {
unsigned int result;

// The + in "+m" denotes a read-modify-write operand.
asm volatile ("lock xaddl %1, %0" :
"+m" (*addr), "=a" (result) :
"1" (newval) :
"cc");
Expand All @@ -203,6 +214,31 @@ static inline void amd64_cpuid(unsigned int ax, unsigned int *p) {
: "0" (ax));
}

static inline uint64 amd64_rdmsr(uint msr) {
uint32 low, high;
asm volatile ("rdmsr"
: "=a" (low), "=d" (high)
: "c" (msr));
return (uint64)low | ((uint64)high << 32);
}

static inline void amd64_wrmsr(uint msr, uint64 newval) {
uint32 low = newval;
uint32 high = newval >> 32;
asm volatile ("wrmsr"
:
: "a" (low), "d" (high), "c" (msr));
}

static inline uint64 amd64_rdtsc()
{
uint32 low, high;

asm volatile("rdtsc" : "=a" (low), "=d" (high));
return (uint64)low | ((uint64)high << 32);
}


// lie about some register names in 64bit mode to avoid
// clunky ifdefs in proc.c and trap.c.
struct trapframe {
Expand Down
Loading