Lab 1
This commit is contained in:
155
kern/COPYRIGHT
Normal file
155
kern/COPYRIGHT
Normal file
@@ -0,0 +1,155 @@
|
||||
Most of the source files in this directory are derived from the Exokernel,
|
||||
which is:
|
||||
|
||||
/*
|
||||
* Copyright (C) 1997 Massachusetts Institute of Technology
|
||||
*
|
||||
* This software is being provided by the copyright holders under the
|
||||
* following license. By obtaining, using and/or copying this software,
|
||||
* you agree that you have read, understood, and will comply with the
|
||||
* following terms and conditions:
|
||||
*
|
||||
* Permission to use, copy, modify, distribute, and sell this software
|
||||
* and its documentation for any purpose and without fee or royalty is
|
||||
* hereby granted, provided that the full text of this NOTICE appears on
|
||||
* ALL copies of the software and documentation or portions thereof,
|
||||
* including modifications, that you make.
|
||||
*
|
||||
* THIS SOFTWARE IS PROVIDED "AS IS," AND COPYRIGHT HOLDERS MAKE NO
|
||||
* REPRESENTATIONS OR WARRANTIES, EXPRESS OR IMPLIED. BY WAY OF EXAMPLE,
|
||||
* BUT NOT LIMITATION, COPYRIGHT HOLDERS MAKE NO REPRESENTATIONS OR
|
||||
* WARRANTIES OF MERCHANTABILITY OR FITNESS FOR ANY PARTICULAR PURPOSE OR
|
||||
* THAT THE USE OF THE SOFTWARE OR DOCUMENTATION WILL NOT INFRINGE ANY
|
||||
* THIRD PARTY PATENTS, COPYRIGHTS, TRADEMARKS OR OTHER RIGHTS. COPYRIGHT
|
||||
* HOLDERS WILL BEAR NO LIABILITY FOR ANY USE OF THIS SOFTWARE OR
|
||||
* DOCUMENTATION.
|
||||
*
|
||||
* The name and trademarks of copyright holders may NOT be used in
|
||||
* advertising or publicity pertaining to the software without specific,
|
||||
* written prior permission. Title to copyright in this software and any
|
||||
* associated documentation will at all times remain with copyright
|
||||
* holders. See the file AUTHORS which should have accompanied this software
|
||||
* for a list of all copyright holders.
|
||||
*
|
||||
* This file may be derived from previously copyrighted software. This
|
||||
* copyright applies only to those changes made by the copyright
|
||||
* holders listed in the AUTHORS file. The rest of this file is covered by
|
||||
* the copyright notices, if any, listed below.
|
||||
*/
|
||||
|
||||
Console.c was created consulting the NetBSD pccons driver which is:
|
||||
|
||||
/*-
|
||||
* Copyright (c) 1993, 1994, 1995 Charles Hannum. All rights reserved.
|
||||
* Copyright (c) 1990 The Regents of the University of California.
|
||||
* All rights reserved.
|
||||
*
|
||||
* This code is derived from software contributed to Berkeley by
|
||||
* William Jolitz and Don Ahn.
|
||||
*
|
||||
* Redistribution and use in source and binary forms, with or without
|
||||
* modification, are permitted provided that the following conditions
|
||||
* are met:
|
||||
* 1. Redistributions of source code must retain the above copyright
|
||||
* notice, this list of conditions and the following disclaimer.
|
||||
* 2. Redistributions in binary form must reproduce the above copyright
|
||||
* notice, this list of conditions and the following disclaimer in the
|
||||
* documentation and/or other materials provided with the distribution.
|
||||
* 3. All advertising materials mentioning features or use of this software
|
||||
* must display the following acknowledgement:
|
||||
* This product includes software developed by the University of
|
||||
* California, Berkeley and its contributors.
|
||||
* 4. Neither the name of the University nor the names of its contributors
|
||||
* may be used to endorse or promote products derived from this software
|
||||
* without specific prior written permission.
|
||||
*
|
||||
* THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
|
||||
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
||||
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
|
||||
* ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
|
||||
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
|
||||
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
|
||||
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
|
||||
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
|
||||
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
|
||||
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
|
||||
* SUCH DAMAGE.
|
||||
*/
|
||||
|
||||
Kclock.h, sched.h, and printf.h are copyright:
|
||||
|
||||
/*
|
||||
* Copyright (C) 1998 Exotec, Inc.
|
||||
*
|
||||
* This software is being provided by the copyright holders under the
|
||||
* following license. By obtaining, using and/or copying this software,
|
||||
* you agree that you have read, understood, and will comply with the
|
||||
* following terms and conditions:
|
||||
*
|
||||
* Permission to use, copy, modify, distribute, and sell this software
|
||||
* and its documentation for any purpose and without fee or royalty is
|
||||
* hereby granted, provided that the full text of this NOTICE appears on
|
||||
* ALL copies of the software and documentation or portions thereof,
|
||||
* including modifications, that you make.
|
||||
*
|
||||
* THIS SOFTWARE IS PROVIDED "AS IS," AND COPYRIGHT HOLDERS MAKE NO
|
||||
* REPRESENTATIONS OR WARRANTIES, EXPRESS OR IMPLIED. BY WAY OF EXAMPLE,
|
||||
* BUT NOT LIMITATION, COPYRIGHT HOLDERS MAKE NO REPRESENTATIONS OR
|
||||
* WARRANTIES OF MERCHANTABILITY OR FITNESS FOR ANY PARTICULAR PURPOSE OR
|
||||
* THAT THE USE OF THE SOFTWARE OR DOCUMENTATION WILL NOT INFRINGE ANY
|
||||
* THIRD PARTY PATENTS, COPYRIGHTS, TRADEMARKS OR OTHER RIGHTS. COPYRIGHT
|
||||
* HOLDERS WILL BEAR NO LIABILITY FOR ANY USE OF THIS SOFTWARE OR
|
||||
* DOCUMENTATION.
|
||||
*
|
||||
* The name and trademarks of copyright holders may NOT be used in
|
||||
* advertising or publicity pertaining to the software without specific,
|
||||
* written prior permission. Title to copyright in this software and any
|
||||
* associated documentation will at all times remain with Exotec, Inc..
|
||||
*
|
||||
* This file may be derived from previously copyrighted software. This
|
||||
* copyright applies only to those changes made by Exotec, Inc. The rest
|
||||
* of this file is covered by the copyright notices, if any, listed below.
|
||||
*/
|
||||
|
||||
Printf.c is copyright:
|
||||
|
||||
/*-
|
||||
* Copyright (c) 1986, 1988, 1991, 1993
|
||||
* The Regents of the University of California. All rights reserved.
|
||||
* (c) UNIX System Laboratories, Inc.
|
||||
* All or some portions of this file are derived from material licensed
|
||||
* to the University of California by American Telephone and Telegraph
|
||||
* Co. or Unix System Laboratories, Inc. and are reproduced herein with
|
||||
* the permission of UNIX System Laboratories, Inc.
|
||||
*
|
||||
* Redistribution and use in source and binary forms, with or without
|
||||
* modification, are permitted provided that the following conditions
|
||||
* are met:
|
||||
* 1. Redistributions of source code must retain the above copyright
|
||||
* notice, this list of conditions and the following disclaimer.
|
||||
* 2. Redistributions in binary form must reproduce the above copyright
|
||||
* notice, this list of conditions and the following disclaimer in the
|
||||
* documentation and/or other materials provided with the distribution.
|
||||
* 3. All advertising materials mentioning features or use of this software
|
||||
* must display the following acknowledgement:
|
||||
* This product includes software developed by the University of
|
||||
* California, Berkeley and its contributors.
|
||||
* 4. Neither the name of the University nor the names of its contributors
|
||||
* may be used to endorse or promote products derived from this software
|
||||
* without specific prior written permission.
|
||||
*
|
||||
* THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
|
||||
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
||||
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
|
||||
* ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
|
||||
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
|
||||
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
|
||||
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
|
||||
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
|
||||
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
|
||||
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
|
||||
* SUCH DAMAGE.
|
||||
*
|
||||
* @(#)subr_prf.c 8.3 (Berkeley) 1/21/94
|
||||
*/
|
||||
|
||||
89
kern/Makefrag
Normal file
89
kern/Makefrag
Normal file
@@ -0,0 +1,89 @@
|
||||
#
|
||||
# Makefile fragment for JOS kernel.
|
||||
# This is NOT a complete makefile;
|
||||
# you must run GNU make in the top-level directory
|
||||
# where the GNUmakefile is located.
|
||||
#
|
||||
|
||||
OBJDIRS += kern
|
||||
|
||||
KERN_LDFLAGS := $(LDFLAGS) -T kern/kernel.ld -nostdlib
|
||||
|
||||
# entry.S must be first, so that it's the first code in the text segment!!!
|
||||
#
|
||||
# We also snatch the use of a couple handy source files
|
||||
# from the lib directory, to avoid gratuitous code duplication.
|
||||
KERN_SRCFILES := kern/entry.S \
|
||||
kern/entrypgdir.c \
|
||||
kern/init.c \
|
||||
kern/console.c \
|
||||
kern/monitor.c \
|
||||
kern/pmap.c \
|
||||
kern/env.c \
|
||||
kern/kclock.c \
|
||||
kern/picirq.c \
|
||||
kern/printf.c \
|
||||
kern/trap.c \
|
||||
kern/trapentry.S \
|
||||
kern/sched.c \
|
||||
kern/syscall.c \
|
||||
kern/kdebug.c \
|
||||
lib/printfmt.c \
|
||||
lib/readline.c \
|
||||
lib/string.c
|
||||
|
||||
# Only build files if they exist.
|
||||
KERN_SRCFILES := $(wildcard $(KERN_SRCFILES))
|
||||
|
||||
# Binary program images to embed within the kernel.
|
||||
KERN_BINFILES :=
|
||||
|
||||
KERN_OBJFILES := $(patsubst %.c, $(OBJDIR)/%.o, $(KERN_SRCFILES))
|
||||
KERN_OBJFILES := $(patsubst %.S, $(OBJDIR)/%.o, $(KERN_OBJFILES))
|
||||
KERN_OBJFILES := $(patsubst $(OBJDIR)/lib/%, $(OBJDIR)/kern/%, $(KERN_OBJFILES))
|
||||
|
||||
KERN_BINFILES := $(patsubst %, $(OBJDIR)/%, $(KERN_BINFILES))
|
||||
|
||||
# How to build kernel object files
|
||||
$(OBJDIR)/kern/%.o: kern/%.c $(OBJDIR)/.vars.KERN_CFLAGS
|
||||
@echo + cc $<
|
||||
@mkdir -p $(@D)
|
||||
$(V)$(CC) -nostdinc $(KERN_CFLAGS) -c -o $@ $<
|
||||
|
||||
$(OBJDIR)/kern/%.o: kern/%.S $(OBJDIR)/.vars.KERN_CFLAGS
|
||||
@echo + as $<
|
||||
@mkdir -p $(@D)
|
||||
$(V)$(CC) -nostdinc $(KERN_CFLAGS) -c -o $@ $<
|
||||
|
||||
$(OBJDIR)/kern/%.o: lib/%.c $(OBJDIR)/.vars.KERN_CFLAGS
|
||||
@echo + cc $<
|
||||
@mkdir -p $(@D)
|
||||
$(V)$(CC) -nostdinc $(KERN_CFLAGS) -c -o $@ $<
|
||||
|
||||
# Special flags for kern/init
|
||||
$(OBJDIR)/kern/init.o: override KERN_CFLAGS+=$(INIT_CFLAGS)
|
||||
$(OBJDIR)/kern/init.o: $(OBJDIR)/.vars.INIT_CFLAGS
|
||||
|
||||
# How to build the kernel itself
|
||||
$(OBJDIR)/kern/kernel: $(KERN_OBJFILES) $(KERN_BINFILES) kern/kernel.ld \
|
||||
$(OBJDIR)/.vars.KERN_LDFLAGS
|
||||
@echo + ld $@
|
||||
$(V)$(LD) -o $@ $(KERN_LDFLAGS) $(KERN_OBJFILES) $(GCC_LIB) -b binary $(KERN_BINFILES)
|
||||
$(V)$(OBJDUMP) -S $@ > $@.asm
|
||||
$(V)$(NM) -n $@ > $@.sym
|
||||
|
||||
# How to build the kernel disk image
|
||||
$(OBJDIR)/kern/kernel.img: $(OBJDIR)/kern/kernel $(OBJDIR)/boot/boot
|
||||
@echo + mk $@
|
||||
$(V)dd if=/dev/zero of=$(OBJDIR)/kern/kernel.img~ count=10000 2>/dev/null
|
||||
$(V)dd if=$(OBJDIR)/boot/boot of=$(OBJDIR)/kern/kernel.img~ conv=notrunc 2>/dev/null
|
||||
$(V)dd if=$(OBJDIR)/kern/kernel of=$(OBJDIR)/kern/kernel.img~ seek=1 conv=notrunc 2>/dev/null
|
||||
$(V)mv $(OBJDIR)/kern/kernel.img~ $(OBJDIR)/kern/kernel.img
|
||||
|
||||
all: $(OBJDIR)/kern/kernel.img
|
||||
|
||||
grub: $(OBJDIR)/jos-grub
|
||||
|
||||
$(OBJDIR)/jos-grub: $(OBJDIR)/kern/kernel
|
||||
@echo + oc $@
|
||||
$(V)$(OBJCOPY) --adjust-vma=0x10000000 $^ $@
|
||||
476
kern/console.c
Normal file
476
kern/console.c
Normal file
@@ -0,0 +1,476 @@
|
||||
/* See COPYRIGHT for copyright information. */
|
||||
|
||||
#include <inc/x86.h>
|
||||
#include <inc/memlayout.h>
|
||||
#include <inc/kbdreg.h>
|
||||
#include <inc/string.h>
|
||||
#include <inc/assert.h>
|
||||
|
||||
#include <kern/console.h>
|
||||
|
||||
static void cons_intr(int (*proc)(void));
|
||||
static void cons_putc(int c);
|
||||
|
||||
// Stupid I/O delay routine necessitated by historical PC design flaws
|
||||
static void
|
||||
delay(void)
|
||||
{
|
||||
inb(0x84);
|
||||
inb(0x84);
|
||||
inb(0x84);
|
||||
inb(0x84);
|
||||
}
|
||||
|
||||
/***** Serial I/O code *****/
|
||||
|
||||
#define COM1 0x3F8
|
||||
|
||||
#define COM_RX 0 // In: Receive buffer (DLAB=0)
|
||||
#define COM_TX 0 // Out: Transmit buffer (DLAB=0)
|
||||
#define COM_DLL 0 // Out: Divisor Latch Low (DLAB=1)
|
||||
#define COM_DLM 1 // Out: Divisor Latch High (DLAB=1)
|
||||
#define COM_IER 1 // Out: Interrupt Enable Register
|
||||
#define COM_IER_RDI 0x01 // Enable receiver data interrupt
|
||||
#define COM_IIR 2 // In: Interrupt ID Register
|
||||
#define COM_FCR 2 // Out: FIFO Control Register
|
||||
#define COM_LCR 3 // Out: Line Control Register
|
||||
#define COM_LCR_DLAB 0x80 // Divisor latch access bit
|
||||
#define COM_LCR_WLEN8 0x03 // Wordlength: 8 bits
|
||||
#define COM_MCR 4 // Out: Modem Control Register
|
||||
#define COM_MCR_RTS 0x02 // RTS complement
|
||||
#define COM_MCR_DTR 0x01 // DTR complement
|
||||
#define COM_MCR_OUT2 0x08 // Out2 complement
|
||||
#define COM_LSR 5 // In: Line Status Register
|
||||
#define COM_LSR_DATA 0x01 // Data available
|
||||
#define COM_LSR_TXRDY 0x20 // Transmit buffer avail
|
||||
#define COM_LSR_TSRE 0x40 // Transmitter off
|
||||
|
||||
static bool serial_exists;
|
||||
|
||||
static int
|
||||
serial_proc_data(void)
|
||||
{
|
||||
if (!(inb(COM1+COM_LSR) & COM_LSR_DATA))
|
||||
return -1;
|
||||
return inb(COM1+COM_RX);
|
||||
}
|
||||
|
||||
void
|
||||
serial_intr(void)
|
||||
{
|
||||
if (serial_exists)
|
||||
cons_intr(serial_proc_data);
|
||||
}
|
||||
|
||||
static void
|
||||
serial_putc(int c)
|
||||
{
|
||||
int i;
|
||||
|
||||
for (i = 0;
|
||||
!(inb(COM1 + COM_LSR) & COM_LSR_TXRDY) && i < 12800;
|
||||
i++)
|
||||
delay();
|
||||
|
||||
outb(COM1 + COM_TX, c);
|
||||
}
|
||||
|
||||
static void
|
||||
serial_init(void)
|
||||
{
|
||||
// Turn off the FIFO
|
||||
outb(COM1+COM_FCR, 0);
|
||||
|
||||
// Set speed; requires DLAB latch
|
||||
outb(COM1+COM_LCR, COM_LCR_DLAB);
|
||||
outb(COM1+COM_DLL, (uint8_t) (115200 / 9600));
|
||||
outb(COM1+COM_DLM, 0);
|
||||
|
||||
// 8 data bits, 1 stop bit, parity off; turn off DLAB latch
|
||||
outb(COM1+COM_LCR, COM_LCR_WLEN8 & ~COM_LCR_DLAB);
|
||||
|
||||
// No modem controls
|
||||
outb(COM1+COM_MCR, 0);
|
||||
// Enable rcv interrupts
|
||||
outb(COM1+COM_IER, COM_IER_RDI);
|
||||
|
||||
// Clear any preexisting overrun indications and interrupts
|
||||
// Serial port doesn't exist if COM_LSR returns 0xFF
|
||||
serial_exists = (inb(COM1+COM_LSR) != 0xFF);
|
||||
(void) inb(COM1+COM_IIR);
|
||||
(void) inb(COM1+COM_RX);
|
||||
|
||||
}
|
||||
|
||||
|
||||
|
||||
/***** Parallel port output code *****/
|
||||
// For information on PC parallel port programming, see the class References
|
||||
// page.
|
||||
|
||||
static void
|
||||
lpt_putc(int c)
|
||||
{
|
||||
int i;
|
||||
|
||||
for (i = 0; !(inb(0x378+1) & 0x80) && i < 12800; i++)
|
||||
delay();
|
||||
outb(0x378+0, c);
|
||||
outb(0x378+2, 0x08|0x04|0x01);
|
||||
outb(0x378+2, 0x08);
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
/***** Text-mode CGA/VGA display output *****/
|
||||
|
||||
static unsigned addr_6845;
|
||||
static uint16_t *crt_buf;
|
||||
static uint16_t crt_pos;
|
||||
|
||||
static void
|
||||
cga_init(void)
|
||||
{
|
||||
volatile uint16_t *cp;
|
||||
uint16_t was;
|
||||
unsigned pos;
|
||||
|
||||
cp = (uint16_t*) (KERNBASE + CGA_BUF);
|
||||
was = *cp;
|
||||
*cp = (uint16_t) 0xA55A;
|
||||
if (*cp != 0xA55A) {
|
||||
cp = (uint16_t*) (KERNBASE + MONO_BUF);
|
||||
addr_6845 = MONO_BASE;
|
||||
} else {
|
||||
*cp = was;
|
||||
addr_6845 = CGA_BASE;
|
||||
}
|
||||
|
||||
/* Extract cursor location */
|
||||
outb(addr_6845, 14);
|
||||
pos = inb(addr_6845 + 1) << 8;
|
||||
outb(addr_6845, 15);
|
||||
pos |= inb(addr_6845 + 1);
|
||||
|
||||
crt_buf = (uint16_t*) cp;
|
||||
crt_pos = pos;
|
||||
}
|
||||
|
||||
|
||||
|
||||
static void
|
||||
cga_putc(int c)
|
||||
{
|
||||
// if no attribute given, then use black on white
|
||||
if (!(c & ~0xFF))
|
||||
c |= 0x0700;
|
||||
|
||||
switch (c & 0xff) {
|
||||
case '\b':
|
||||
if (crt_pos > 0) {
|
||||
crt_pos--;
|
||||
crt_buf[crt_pos] = (c & ~0xff) | ' ';
|
||||
}
|
||||
break;
|
||||
case '\n':
|
||||
crt_pos += CRT_COLS;
|
||||
/* fallthru */
|
||||
case '\r':
|
||||
crt_pos -= (crt_pos % CRT_COLS);
|
||||
break;
|
||||
case '\t':
|
||||
cons_putc(' ');
|
||||
cons_putc(' ');
|
||||
cons_putc(' ');
|
||||
cons_putc(' ');
|
||||
cons_putc(' ');
|
||||
break;
|
||||
default:
|
||||
crt_buf[crt_pos++] = c; /* write the character */
|
||||
break;
|
||||
}
|
||||
|
||||
// What is the purpose of this?
|
||||
if (crt_pos >= CRT_SIZE) {
|
||||
int i;
|
||||
|
||||
memmove(crt_buf, crt_buf + CRT_COLS, (CRT_SIZE - CRT_COLS) * sizeof(uint16_t));
|
||||
for (i = CRT_SIZE - CRT_COLS; i < CRT_SIZE; i++)
|
||||
crt_buf[i] = 0x0700 | ' ';
|
||||
crt_pos -= CRT_COLS;
|
||||
}
|
||||
|
||||
/* move that little blinky thing */
|
||||
outb(addr_6845, 14);
|
||||
outb(addr_6845 + 1, crt_pos >> 8);
|
||||
outb(addr_6845, 15);
|
||||
outb(addr_6845 + 1, crt_pos);
|
||||
}
|
||||
|
||||
|
||||
/***** Keyboard input code *****/
|
||||
|
||||
#define NO 0
|
||||
|
||||
#define SHIFT (1<<0)
|
||||
#define CTL (1<<1)
|
||||
#define ALT (1<<2)
|
||||
|
||||
#define CAPSLOCK (1<<3)
|
||||
#define NUMLOCK (1<<4)
|
||||
#define SCROLLLOCK (1<<5)
|
||||
|
||||
#define E0ESC (1<<6)
|
||||
|
||||
static uint8_t shiftcode[256] =
|
||||
{
|
||||
[0x1D] = CTL,
|
||||
[0x2A] = SHIFT,
|
||||
[0x36] = SHIFT,
|
||||
[0x38] = ALT,
|
||||
[0x9D] = CTL,
|
||||
[0xB8] = ALT
|
||||
};
|
||||
|
||||
static uint8_t togglecode[256] =
|
||||
{
|
||||
[0x3A] = CAPSLOCK,
|
||||
[0x45] = NUMLOCK,
|
||||
[0x46] = SCROLLLOCK
|
||||
};
|
||||
|
||||
static uint8_t normalmap[256] =
|
||||
{
|
||||
NO, 0x1B, '1', '2', '3', '4', '5', '6', // 0x00
|
||||
'7', '8', '9', '0', '-', '=', '\b', '\t',
|
||||
'q', 'w', 'e', 'r', 't', 'y', 'u', 'i', // 0x10
|
||||
'o', 'p', '[', ']', '\n', NO, 'a', 's',
|
||||
'd', 'f', 'g', 'h', 'j', 'k', 'l', ';', // 0x20
|
||||
'\'', '`', NO, '\\', 'z', 'x', 'c', 'v',
|
||||
'b', 'n', 'm', ',', '.', '/', NO, '*', // 0x30
|
||||
NO, ' ', NO, NO, NO, NO, NO, NO,
|
||||
NO, NO, NO, NO, NO, NO, NO, '7', // 0x40
|
||||
'8', '9', '-', '4', '5', '6', '+', '1',
|
||||
'2', '3', '0', '.', NO, NO, NO, NO, // 0x50
|
||||
[0xC7] = KEY_HOME, [0x9C] = '\n' /*KP_Enter*/,
|
||||
[0xB5] = '/' /*KP_Div*/, [0xC8] = KEY_UP,
|
||||
[0xC9] = KEY_PGUP, [0xCB] = KEY_LF,
|
||||
[0xCD] = KEY_RT, [0xCF] = KEY_END,
|
||||
[0xD0] = KEY_DN, [0xD1] = KEY_PGDN,
|
||||
[0xD2] = KEY_INS, [0xD3] = KEY_DEL
|
||||
};
|
||||
|
||||
static uint8_t shiftmap[256] =
|
||||
{
|
||||
NO, 033, '!', '@', '#', '$', '%', '^', // 0x00
|
||||
'&', '*', '(', ')', '_', '+', '\b', '\t',
|
||||
'Q', 'W', 'E', 'R', 'T', 'Y', 'U', 'I', // 0x10
|
||||
'O', 'P', '{', '}', '\n', NO, 'A', 'S',
|
||||
'D', 'F', 'G', 'H', 'J', 'K', 'L', ':', // 0x20
|
||||
'"', '~', NO, '|', 'Z', 'X', 'C', 'V',
|
||||
'B', 'N', 'M', '<', '>', '?', NO, '*', // 0x30
|
||||
NO, ' ', NO, NO, NO, NO, NO, NO,
|
||||
NO, NO, NO, NO, NO, NO, NO, '7', // 0x40
|
||||
'8', '9', '-', '4', '5', '6', '+', '1',
|
||||
'2', '3', '0', '.', NO, NO, NO, NO, // 0x50
|
||||
[0xC7] = KEY_HOME, [0x9C] = '\n' /*KP_Enter*/,
|
||||
[0xB5] = '/' /*KP_Div*/, [0xC8] = KEY_UP,
|
||||
[0xC9] = KEY_PGUP, [0xCB] = KEY_LF,
|
||||
[0xCD] = KEY_RT, [0xCF] = KEY_END,
|
||||
[0xD0] = KEY_DN, [0xD1] = KEY_PGDN,
|
||||
[0xD2] = KEY_INS, [0xD3] = KEY_DEL
|
||||
};
|
||||
|
||||
#define C(x) (x - '@')
|
||||
|
||||
static uint8_t ctlmap[256] =
|
||||
{
|
||||
NO, NO, NO, NO, NO, NO, NO, NO,
|
||||
NO, NO, NO, NO, NO, NO, NO, NO,
|
||||
C('Q'), C('W'), C('E'), C('R'), C('T'), C('Y'), C('U'), C('I'),
|
||||
C('O'), C('P'), NO, NO, '\r', NO, C('A'), C('S'),
|
||||
C('D'), C('F'), C('G'), C('H'), C('J'), C('K'), C('L'), NO,
|
||||
NO, NO, NO, C('\\'), C('Z'), C('X'), C('C'), C('V'),
|
||||
C('B'), C('N'), C('M'), NO, NO, C('/'), NO, NO,
|
||||
[0x97] = KEY_HOME,
|
||||
[0xB5] = C('/'), [0xC8] = KEY_UP,
|
||||
[0xC9] = KEY_PGUP, [0xCB] = KEY_LF,
|
||||
[0xCD] = KEY_RT, [0xCF] = KEY_END,
|
||||
[0xD0] = KEY_DN, [0xD1] = KEY_PGDN,
|
||||
[0xD2] = KEY_INS, [0xD3] = KEY_DEL
|
||||
};
|
||||
|
||||
static uint8_t *charcode[4] = {
|
||||
normalmap,
|
||||
shiftmap,
|
||||
ctlmap,
|
||||
ctlmap
|
||||
};
|
||||
|
||||
/*
|
||||
* Get data from the keyboard. If we finish a character, return it. Else 0.
|
||||
* Return -1 if no data.
|
||||
*/
|
||||
static int
|
||||
kbd_proc_data(void)
|
||||
{
|
||||
int c;
|
||||
uint8_t stat, data;
|
||||
static uint32_t shift;
|
||||
|
||||
stat = inb(KBSTATP);
|
||||
if ((stat & KBS_DIB) == 0)
|
||||
return -1;
|
||||
// Ignore data from mouse.
|
||||
if (stat & KBS_TERR)
|
||||
return -1;
|
||||
|
||||
data = inb(KBDATAP);
|
||||
|
||||
if (data == 0xE0) {
|
||||
// E0 escape character
|
||||
shift |= E0ESC;
|
||||
return 0;
|
||||
} else if (data & 0x80) {
|
||||
// Key released
|
||||
data = (shift & E0ESC ? data : data & 0x7F);
|
||||
shift &= ~(shiftcode[data] | E0ESC);
|
||||
return 0;
|
||||
} else if (shift & E0ESC) {
|
||||
// Last character was an E0 escape; or with 0x80
|
||||
data |= 0x80;
|
||||
shift &= ~E0ESC;
|
||||
}
|
||||
|
||||
shift |= shiftcode[data];
|
||||
shift ^= togglecode[data];
|
||||
|
||||
c = charcode[shift & (CTL | SHIFT)][data];
|
||||
if (shift & CAPSLOCK) {
|
||||
if ('a' <= c && c <= 'z')
|
||||
c += 'A' - 'a';
|
||||
else if ('A' <= c && c <= 'Z')
|
||||
c += 'a' - 'A';
|
||||
}
|
||||
|
||||
// Process special keys
|
||||
// Ctrl-Alt-Del: reboot
|
||||
if (!(~shift & (CTL | ALT)) && c == KEY_DEL) {
|
||||
cprintf("Rebooting!\n");
|
||||
outb(0x92, 0x3); // courtesy of Chris Frost
|
||||
}
|
||||
|
||||
return c;
|
||||
}
|
||||
|
||||
void
|
||||
kbd_intr(void)
|
||||
{
|
||||
cons_intr(kbd_proc_data);
|
||||
}
|
||||
|
||||
static void
|
||||
kbd_init(void)
|
||||
{
|
||||
}
|
||||
|
||||
|
||||
|
||||
/***** General device-independent console code *****/
|
||||
// Here we manage the console input buffer,
|
||||
// where we stash characters received from the keyboard or serial port
|
||||
// whenever the corresponding interrupt occurs.
|
||||
|
||||
#define CONSBUFSIZE 512
|
||||
|
||||
static struct {
|
||||
uint8_t buf[CONSBUFSIZE];
|
||||
uint32_t rpos;
|
||||
uint32_t wpos;
|
||||
} cons;
|
||||
|
||||
// called by device interrupt routines to feed input characters
|
||||
// into the circular console input buffer.
|
||||
static void
|
||||
cons_intr(int (*proc)(void))
|
||||
{
|
||||
int c;
|
||||
|
||||
while ((c = (*proc)()) != -1) {
|
||||
if (c == 0)
|
||||
continue;
|
||||
cons.buf[cons.wpos++] = c;
|
||||
if (cons.wpos == CONSBUFSIZE)
|
||||
cons.wpos = 0;
|
||||
}
|
||||
}
|
||||
|
||||
// return the next input character from the console, or 0 if none waiting
|
||||
int
|
||||
cons_getc(void)
|
||||
{
|
||||
int c;
|
||||
|
||||
// poll for any pending input characters,
|
||||
// so that this function works even when interrupts are disabled
|
||||
// (e.g., when called from the kernel monitor).
|
||||
serial_intr();
|
||||
kbd_intr();
|
||||
|
||||
// grab the next character from the input buffer.
|
||||
if (cons.rpos != cons.wpos) {
|
||||
c = cons.buf[cons.rpos++];
|
||||
if (cons.rpos == CONSBUFSIZE)
|
||||
cons.rpos = 0;
|
||||
return c;
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
// output a character to the console
|
||||
static void
|
||||
cons_putc(int c)
|
||||
{
|
||||
serial_putc(c);
|
||||
lpt_putc(c);
|
||||
cga_putc(c);
|
||||
}
|
||||
|
||||
// initialize the console devices
|
||||
void
|
||||
cons_init(void)
|
||||
{
|
||||
cga_init();
|
||||
kbd_init();
|
||||
serial_init();
|
||||
|
||||
if (!serial_exists)
|
||||
cprintf("Serial port does not exist!\n");
|
||||
}
|
||||
|
||||
|
||||
// `High'-level console I/O. Used by readline and cprintf.
|
||||
|
||||
void
|
||||
cputchar(int c)
|
||||
{
|
||||
cons_putc(c);
|
||||
}
|
||||
|
||||
int
|
||||
getchar(void)
|
||||
{
|
||||
int c;
|
||||
|
||||
while ((c = cons_getc()) == 0)
|
||||
/* do nothing */;
|
||||
return c;
|
||||
}
|
||||
|
||||
int
|
||||
iscons(int fdnum)
|
||||
{
|
||||
// used by readline
|
||||
return 1;
|
||||
}
|
||||
26
kern/console.h
Normal file
26
kern/console.h
Normal file
@@ -0,0 +1,26 @@
|
||||
/* See COPYRIGHT for copyright information. */
|
||||
|
||||
#ifndef _CONSOLE_H_
|
||||
#define _CONSOLE_H_
|
||||
#ifndef JOS_KERNEL
|
||||
# error "This is a JOS kernel header; user programs should not #include it"
|
||||
#endif
|
||||
|
||||
#include <inc/types.h>
|
||||
|
||||
#define MONO_BASE 0x3B4
|
||||
#define MONO_BUF 0xB0000
|
||||
#define CGA_BASE 0x3D4
|
||||
#define CGA_BUF 0xB8000
|
||||
|
||||
#define CRT_ROWS 25
|
||||
#define CRT_COLS 80
|
||||
#define CRT_SIZE (CRT_ROWS * CRT_COLS)
|
||||
|
||||
void cons_init(void);
|
||||
int cons_getc(void);
|
||||
|
||||
void kbd_intr(void); // irq 1
|
||||
void serial_intr(void); // irq 4
|
||||
|
||||
#endif /* _CONSOLE_H_ */
|
||||
96
kern/entry.S
Normal file
96
kern/entry.S
Normal file
@@ -0,0 +1,96 @@
|
||||
/* See COPYRIGHT for copyright information. */
|
||||
|
||||
#include <inc/mmu.h>
|
||||
#include <inc/memlayout.h>
|
||||
|
||||
# Shift Right Logical
|
||||
#define SRL(val, shamt) (((val) >> (shamt)) & ~(-1 << (32 - (shamt))))
|
||||
|
||||
|
||||
###################################################################
|
||||
# The kernel (this code) is linked at address ~(KERNBASE + 1 Meg),
|
||||
# but the bootloader loads it at address ~1 Meg.
|
||||
#
|
||||
# RELOC(x) maps a symbol x from its link address to its actual
|
||||
# location in physical memory (its load address).
|
||||
###################################################################
|
||||
|
||||
#define RELOC(x) ((x) - KERNBASE)
|
||||
|
||||
#define MULTIBOOT_HEADER_MAGIC (0x1BADB002)
|
||||
#define MULTIBOOT_HEADER_FLAGS (0)
|
||||
#define CHECKSUM (-(MULTIBOOT_HEADER_MAGIC + MULTIBOOT_HEADER_FLAGS))
|
||||
|
||||
###################################################################
|
||||
# entry point
|
||||
###################################################################
|
||||
|
||||
.text
|
||||
|
||||
# The Multiboot header
|
||||
.align 4
|
||||
.long MULTIBOOT_HEADER_MAGIC
|
||||
.long MULTIBOOT_HEADER_FLAGS
|
||||
.long CHECKSUM
|
||||
|
||||
# '_start' specifies the ELF entry point. Since we haven't set up
|
||||
# virtual memory when the bootloader enters this code, we need the
|
||||
# bootloader to jump to the *physical* address of the entry point.
|
||||
.globl _start
|
||||
_start = RELOC(entry)
|
||||
|
||||
.globl entry
|
||||
entry:
|
||||
movw $0x1234,0x472 # warm boot
|
||||
|
||||
# We haven't set up virtual memory yet, so we're running from
|
||||
# the physical address the boot loader loaded the kernel at: 1MB
|
||||
# (plus a few bytes). However, the C code is linked to run at
|
||||
# KERNBASE+1MB. Hence, we set up a trivial page directory that
|
||||
# translates virtual addresses [KERNBASE, KERNBASE+4MB) to
|
||||
# physical addresses [0, 4MB). This 4MB region will be
|
||||
# sufficient until we set up our real page table in mem_init
|
||||
# in lab 2.
|
||||
|
||||
# Load the physical address of entry_pgdir into cr3. entry_pgdir
|
||||
# is defined in entrypgdir.c.
|
||||
movl $(RELOC(entry_pgdir)), %eax
|
||||
movl %eax, %cr3
|
||||
# Turn on paging.
|
||||
movl %cr0, %eax
|
||||
orl $(CR0_PE|CR0_PG|CR0_WP), %eax
|
||||
movl %eax, %cr0
|
||||
|
||||
# Now paging is enabled, but we're still running at a low EIP
|
||||
# (why is this okay?). Jump up above KERNBASE before entering
|
||||
# C code.
|
||||
mov $relocated, %eax
|
||||
jmp *%eax
|
||||
relocated:
|
||||
|
||||
# Clear the frame pointer register (EBP)
|
||||
# so that once we get into debugging C code,
|
||||
# stack backtraces will be terminated properly.
|
||||
movl $0x0,%ebp # nuke frame pointer
|
||||
|
||||
# Set the stack pointer
|
||||
movl $(bootstacktop),%esp
|
||||
|
||||
# now to C code
|
||||
call i386_init
|
||||
|
||||
# Should never get here, but in case we do, just spin.
|
||||
spin: jmp spin
|
||||
|
||||
|
||||
.data
|
||||
###################################################################
|
||||
# boot stack
|
||||
###################################################################
|
||||
.p2align PGSHIFT # force page alignment
|
||||
.globl bootstack
|
||||
bootstack:
|
||||
.space KSTKSIZE
|
||||
.globl bootstacktop
|
||||
bootstacktop:
|
||||
|
||||
1059
kern/entrypgdir.c
Normal file
1059
kern/entrypgdir.c
Normal file
File diff suppressed because it is too large
Load Diff
92
kern/init.c
Normal file
92
kern/init.c
Normal file
@@ -0,0 +1,92 @@
|
||||
/* See COPYRIGHT for copyright information. */
|
||||
|
||||
#include <inc/stdio.h>
|
||||
#include <inc/string.h>
|
||||
#include <inc/assert.h>
|
||||
|
||||
#include <kern/monitor.h>
|
||||
#include <kern/console.h>
|
||||
|
||||
// Test the stack backtrace function (lab 1 only)
|
||||
void
|
||||
test_backtrace(int x)
|
||||
{
|
||||
cprintf("entering test_backtrace %d\n", x);
|
||||
if (x > 0)
|
||||
test_backtrace(x-1);
|
||||
else
|
||||
mon_backtrace(0, 0, 0);
|
||||
cprintf("leaving test_backtrace %d\n", x);
|
||||
}
|
||||
|
||||
void
|
||||
i386_init(void)
|
||||
{
|
||||
extern char edata[], end[];
|
||||
|
||||
// Before doing anything else, complete the ELF loading process.
|
||||
// Clear the uninitialized global data (BSS) section of our program.
|
||||
// This ensures that all static/global variables start out zero.
|
||||
memset(edata, 0, end - edata);
|
||||
|
||||
// Initialize the console.
|
||||
// Can't call cprintf until after we do this!
|
||||
cons_init();
|
||||
|
||||
cprintf("6828 decimal is %o octal!\n", 6828);
|
||||
|
||||
// Test the stack backtrace function (lab 1 only)
|
||||
test_backtrace(5);
|
||||
|
||||
// Drop into the kernel monitor.
|
||||
while (1)
|
||||
monitor(NULL);
|
||||
}
|
||||
|
||||
|
||||
/*
|
||||
* Variable panicstr contains argument to first call to panic; used as flag
|
||||
* to indicate that the kernel has already called panic.
|
||||
*/
|
||||
const char *panicstr;
|
||||
|
||||
/*
|
||||
* Panic is called on unresolvable fatal errors.
|
||||
* It prints "panic: mesg", and then enters the kernel monitor.
|
||||
*/
|
||||
void
|
||||
_panic(const char *file, int line, const char *fmt,...)
|
||||
{
|
||||
va_list ap;
|
||||
|
||||
if (panicstr)
|
||||
goto dead;
|
||||
panicstr = fmt;
|
||||
|
||||
// Be extra sure that the machine is in as reasonable state
|
||||
asm volatile("cli; cld");
|
||||
|
||||
va_start(ap, fmt);
|
||||
cprintf("kernel panic at %s:%d: ", file, line);
|
||||
vcprintf(fmt, ap);
|
||||
cprintf("\n");
|
||||
va_end(ap);
|
||||
|
||||
dead:
|
||||
/* break into the kernel monitor */
|
||||
while (1)
|
||||
monitor(NULL);
|
||||
}
|
||||
|
||||
/* like panic, but don't */
|
||||
void
|
||||
_warn(const char *file, int line, const char *fmt,...)
|
||||
{
|
||||
va_list ap;
|
||||
|
||||
va_start(ap, fmt);
|
||||
cprintf("kernel warning at %s:%d: ", file, line);
|
||||
vcprintf(fmt, ap);
|
||||
cprintf("\n");
|
||||
va_end(ap);
|
||||
}
|
||||
206
kern/kdebug.c
Normal file
206
kern/kdebug.c
Normal file
@@ -0,0 +1,206 @@
|
||||
#include <inc/stab.h>
|
||||
#include <inc/string.h>
|
||||
#include <inc/memlayout.h>
|
||||
#include <inc/assert.h>
|
||||
|
||||
#include <kern/kdebug.h>
|
||||
|
||||
extern const struct Stab __STAB_BEGIN__[]; // Beginning of stabs table
|
||||
extern const struct Stab __STAB_END__[]; // End of stabs table
|
||||
extern const char __STABSTR_BEGIN__[]; // Beginning of string table
|
||||
extern const char __STABSTR_END__[]; // End of string table
|
||||
|
||||
|
||||
// stab_binsearch(stabs, region_left, region_right, type, addr)
|
||||
//
|
||||
// Some stab types are arranged in increasing order by instruction
|
||||
// address. For example, N_FUN stabs (stab entries with n_type ==
|
||||
// N_FUN), which mark functions, and N_SO stabs, which mark source files.
|
||||
//
|
||||
// Given an instruction address, this function finds the single stab
|
||||
// entry of type 'type' that contains that address.
|
||||
//
|
||||
// The search takes place within the range [*region_left, *region_right].
|
||||
// Thus, to search an entire set of N stabs, you might do:
|
||||
//
|
||||
// left = 0;
|
||||
// right = N - 1; /* rightmost stab */
|
||||
// stab_binsearch(stabs, &left, &right, type, addr);
|
||||
//
|
||||
// The search modifies *region_left and *region_right to bracket the
|
||||
// 'addr'. *region_left points to the matching stab that contains
|
||||
// 'addr', and *region_right points just before the next stab. If
|
||||
// *region_left > *region_right, then 'addr' is not contained in any
|
||||
// matching stab.
|
||||
//
|
||||
// For example, given these N_SO stabs:
|
||||
// Index Type Address
|
||||
// 0 SO f0100000
|
||||
// 13 SO f0100040
|
||||
// 117 SO f0100176
|
||||
// 118 SO f0100178
|
||||
// 555 SO f0100652
|
||||
// 556 SO f0100654
|
||||
// 657 SO f0100849
|
||||
// this code:
|
||||
// left = 0, right = 657;
|
||||
// stab_binsearch(stabs, &left, &right, N_SO, 0xf0100184);
|
||||
// will exit setting left = 118, right = 554.
|
||||
//
|
||||
static void
|
||||
stab_binsearch(const struct Stab *stabs, int *region_left, int *region_right,
|
||||
int type, uintptr_t addr)
|
||||
{
|
||||
int l = *region_left, r = *region_right, any_matches = 0;
|
||||
|
||||
while (l <= r) {
|
||||
int true_m = (l + r) / 2, m = true_m;
|
||||
|
||||
// search for earliest stab with right type
|
||||
while (m >= l && stabs[m].n_type != type)
|
||||
m--;
|
||||
if (m < l) { // no match in [l, m]
|
||||
l = true_m + 1;
|
||||
continue;
|
||||
}
|
||||
|
||||
// actual binary search
|
||||
any_matches = 1;
|
||||
if (stabs[m].n_value < addr) {
|
||||
*region_left = m;
|
||||
l = true_m + 1;
|
||||
} else if (stabs[m].n_value > addr) {
|
||||
*region_right = m - 1;
|
||||
r = m - 1;
|
||||
} else {
|
||||
// exact match for 'addr', but continue loop to find
|
||||
// *region_right
|
||||
*region_left = m;
|
||||
l = m;
|
||||
addr++;
|
||||
}
|
||||
}
|
||||
|
||||
if (!any_matches)
|
||||
*region_right = *region_left - 1;
|
||||
else {
|
||||
// find rightmost region containing 'addr'
|
||||
for (l = *region_right;
|
||||
l > *region_left && stabs[l].n_type != type;
|
||||
l--)
|
||||
/* do nothing */;
|
||||
*region_left = l;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
// debuginfo_eip(addr, info)
|
||||
//
|
||||
// Fill in the 'info' structure with information about the specified
|
||||
// instruction address, 'addr'. Returns 0 if information was found, and
|
||||
// negative if not. But even if it returns negative it has stored some
|
||||
// information into '*info'.
|
||||
//
|
||||
int
|
||||
debuginfo_eip(uintptr_t addr, struct Eipdebuginfo *info)
|
||||
{
|
||||
const struct Stab *stabs, *stab_end;
|
||||
const char *stabstr, *stabstr_end;
|
||||
int lfile, rfile, lfun, rfun, lline, rline;
|
||||
|
||||
// Initialize *info
|
||||
info->eip_file = "<unknown>";
|
||||
info->eip_line = 0;
|
||||
info->eip_fn_name = "<unknown>";
|
||||
info->eip_fn_namelen = 9;
|
||||
info->eip_fn_addr = addr;
|
||||
info->eip_fn_narg = 0;
|
||||
|
||||
// Find the relevant set of stabs
|
||||
if (addr >= ULIM) {
|
||||
stabs = __STAB_BEGIN__;
|
||||
stab_end = __STAB_END__;
|
||||
stabstr = __STABSTR_BEGIN__;
|
||||
stabstr_end = __STABSTR_END__;
|
||||
} else {
|
||||
// Can't search for user-level addresses yet!
|
||||
panic("User address");
|
||||
}
|
||||
|
||||
// String table validity checks
|
||||
if (stabstr_end <= stabstr || stabstr_end[-1] != 0)
|
||||
return -1;
|
||||
|
||||
// Now we find the right stabs that define the function containing
|
||||
// 'eip'. First, we find the basic source file containing 'eip'.
|
||||
// Then, we look in that source file for the function. Then we look
|
||||
// for the line number.
|
||||
|
||||
// Search the entire set of stabs for the source file (type N_SO).
|
||||
lfile = 0;
|
||||
rfile = (stab_end - stabs) - 1;
|
||||
stab_binsearch(stabs, &lfile, &rfile, N_SO, addr);
|
||||
if (lfile == 0)
|
||||
return -1;
|
||||
|
||||
// Search within that file's stabs for the function definition
|
||||
// (N_FUN).
|
||||
lfun = lfile;
|
||||
rfun = rfile;
|
||||
stab_binsearch(stabs, &lfun, &rfun, N_FUN, addr);
|
||||
|
||||
if (lfun <= rfun) {
|
||||
// stabs[lfun] points to the function name
|
||||
// in the string table, but check bounds just in case.
|
||||
if (stabs[lfun].n_strx < stabstr_end - stabstr)
|
||||
info->eip_fn_name = stabstr + stabs[lfun].n_strx;
|
||||
info->eip_fn_addr = stabs[lfun].n_value;
|
||||
addr -= info->eip_fn_addr;
|
||||
// Search within the function definition for the line number.
|
||||
lline = lfun;
|
||||
rline = rfun;
|
||||
} else {
|
||||
// Couldn't find function stab! Maybe we're in an assembly
|
||||
// file. Search the whole file for the line number.
|
||||
info->eip_fn_addr = addr;
|
||||
lline = lfile;
|
||||
rline = rfile;
|
||||
}
|
||||
// Ignore stuff after the colon.
|
||||
info->eip_fn_namelen = strfind(info->eip_fn_name, ':') - info->eip_fn_name;
|
||||
|
||||
|
||||
// Search within [lline, rline] for the line number stab.
|
||||
// If found, set info->eip_line to the right line number.
|
||||
// If not found, return -1.
|
||||
//
|
||||
// Hint:
|
||||
// There's a particular stabs type used for line numbers.
|
||||
// Look at the STABS documentation and <inc/stab.h> to find
|
||||
// which one.
|
||||
// Your code here.
|
||||
|
||||
|
||||
// Search backwards from the line number for the relevant filename
|
||||
// stab.
|
||||
// We can't just use the "lfile" stab because inlined functions
|
||||
// can interpolate code from a different file!
|
||||
// Such included source files use the N_SOL stab type.
|
||||
while (lline >= lfile
|
||||
&& stabs[lline].n_type != N_SOL
|
||||
&& (stabs[lline].n_type != N_SO || !stabs[lline].n_value))
|
||||
lline--;
|
||||
if (lline >= lfile && stabs[lline].n_strx < stabstr_end - stabstr)
|
||||
info->eip_file = stabstr + stabs[lline].n_strx;
|
||||
|
||||
|
||||
// Set eip_fn_narg to the number of arguments taken by the function,
|
||||
// or 0 if there was no containing function.
|
||||
if (lfun < rfun)
|
||||
for (lline = lfun + 1;
|
||||
lline < rfun && stabs[lline].n_type == N_PSYM;
|
||||
lline++)
|
||||
info->eip_fn_narg++;
|
||||
|
||||
return 0;
|
||||
}
|
||||
20
kern/kdebug.h
Normal file
20
kern/kdebug.h
Normal file
@@ -0,0 +1,20 @@
|
||||
#ifndef JOS_KERN_KDEBUG_H
|
||||
#define JOS_KERN_KDEBUG_H
|
||||
|
||||
#include <inc/types.h>
|
||||
|
||||
// Debug information about a particular instruction pointer
|
||||
struct Eipdebuginfo {
|
||||
const char *eip_file; // Source code filename for EIP
|
||||
int eip_line; // Source code linenumber for EIP
|
||||
|
||||
const char *eip_fn_name; // Name of function containing EIP
|
||||
// - Note: not null terminated!
|
||||
int eip_fn_namelen; // Length of function name
|
||||
uintptr_t eip_fn_addr; // Address of start of function
|
||||
int eip_fn_narg; // Number of function arguments
|
||||
};
|
||||
|
||||
int debuginfo_eip(uintptr_t eip, struct Eipdebuginfo *info);
|
||||
|
||||
#endif
|
||||
61
kern/kernel.ld
Normal file
61
kern/kernel.ld
Normal file
@@ -0,0 +1,61 @@
|
||||
/* Simple linker script for the JOS kernel.
|
||||
See the GNU ld 'info' manual ("info ld") to learn the syntax. */
|
||||
|
||||
OUTPUT_FORMAT("elf32-i386", "elf32-i386", "elf32-i386")
|
||||
OUTPUT_ARCH(i386)
|
||||
ENTRY(_start)
|
||||
|
||||
SECTIONS
|
||||
{
|
||||
/* Link the kernel at this address: "." means the current address */
|
||||
. = 0xF0100000;
|
||||
|
||||
/* AT(...) gives the load address of this section, which tells
|
||||
the boot loader where to load the kernel in physical memory */
|
||||
.text : AT(0x100000) {
|
||||
*(.text .stub .text.* .gnu.linkonce.t.*)
|
||||
}
|
||||
|
||||
PROVIDE(etext = .); /* Define the 'etext' symbol to this value */
|
||||
|
||||
.rodata : {
|
||||
*(.rodata .rodata.* .gnu.linkonce.r.*)
|
||||
}
|
||||
|
||||
/* Include debugging information in kernel memory */
|
||||
.stab : {
|
||||
PROVIDE(__STAB_BEGIN__ = .);
|
||||
*(.stab);
|
||||
PROVIDE(__STAB_END__ = .);
|
||||
BYTE(0) /* Force the linker to allocate space
|
||||
for this section */
|
||||
}
|
||||
|
||||
.stabstr : {
|
||||
PROVIDE(__STABSTR_BEGIN__ = .);
|
||||
*(.stabstr);
|
||||
PROVIDE(__STABSTR_END__ = .);
|
||||
BYTE(0) /* Force the linker to allocate space
|
||||
for this section */
|
||||
}
|
||||
|
||||
/* Adjust the address for the data segment to the next page */
|
||||
. = ALIGN(0x1000);
|
||||
|
||||
/* The data segment */
|
||||
.data : {
|
||||
*(.data)
|
||||
}
|
||||
|
||||
PROVIDE(edata = .);
|
||||
|
||||
.bss : {
|
||||
*(.bss)
|
||||
}
|
||||
|
||||
PROVIDE(end = .);
|
||||
|
||||
/DISCARD/ : {
|
||||
*(.eh_frame .note.GNU-stack)
|
||||
}
|
||||
}
|
||||
125
kern/monitor.c
Normal file
125
kern/monitor.c
Normal file
@@ -0,0 +1,125 @@
|
||||
// Simple command-line kernel monitor useful for
|
||||
// controlling the kernel and exploring the system interactively.
|
||||
|
||||
#include <inc/stdio.h>
|
||||
#include <inc/string.h>
|
||||
#include <inc/memlayout.h>
|
||||
#include <inc/assert.h>
|
||||
#include <inc/x86.h>
|
||||
|
||||
#include <kern/console.h>
|
||||
#include <kern/monitor.h>
|
||||
#include <kern/kdebug.h>
|
||||
|
||||
#define CMDBUF_SIZE 80 // enough for one VGA text line
|
||||
|
||||
|
||||
struct Command {
|
||||
const char *name;
|
||||
const char *desc;
|
||||
// return -1 to force monitor to exit
|
||||
int (*func)(int argc, char** argv, struct Trapframe* tf);
|
||||
};
|
||||
|
||||
static struct Command commands[] = {
|
||||
{ "help", "Display this list of commands", mon_help },
|
||||
{ "kerninfo", "Display information about the kernel", mon_kerninfo },
|
||||
};
|
||||
|
||||
/***** Implementations of basic kernel monitor commands *****/
|
||||
|
||||
int
|
||||
mon_help(int argc, char **argv, struct Trapframe *tf)
|
||||
{
|
||||
int i;
|
||||
|
||||
for (i = 0; i < ARRAY_SIZE(commands); i++)
|
||||
cprintf("%s - %s\n", commands[i].name, commands[i].desc);
|
||||
return 0;
|
||||
}
|
||||
|
||||
int
|
||||
mon_kerninfo(int argc, char **argv, struct Trapframe *tf)
|
||||
{
|
||||
extern char _start[], entry[], etext[], edata[], end[];
|
||||
|
||||
cprintf("Special kernel symbols:\n");
|
||||
cprintf(" _start %08x (phys)\n", _start);
|
||||
cprintf(" entry %08x (virt) %08x (phys)\n", entry, entry - KERNBASE);
|
||||
cprintf(" etext %08x (virt) %08x (phys)\n", etext, etext - KERNBASE);
|
||||
cprintf(" edata %08x (virt) %08x (phys)\n", edata, edata - KERNBASE);
|
||||
cprintf(" end %08x (virt) %08x (phys)\n", end, end - KERNBASE);
|
||||
cprintf("Kernel executable memory footprint: %dKB\n",
|
||||
ROUNDUP(end - entry, 1024) / 1024);
|
||||
return 0;
|
||||
}
|
||||
|
||||
int
|
||||
mon_backtrace(int argc, char **argv, struct Trapframe *tf)
|
||||
{
|
||||
// Your code here.
|
||||
return 0;
|
||||
}
|
||||
|
||||
|
||||
|
||||
/***** Kernel monitor command interpreter *****/
|
||||
|
||||
#define WHITESPACE "\t\r\n "
|
||||
#define MAXARGS 16
|
||||
|
||||
static int
|
||||
runcmd(char *buf, struct Trapframe *tf)
|
||||
{
|
||||
int argc;
|
||||
char *argv[MAXARGS];
|
||||
int i;
|
||||
|
||||
// Parse the command buffer into whitespace-separated arguments
|
||||
argc = 0;
|
||||
argv[argc] = 0;
|
||||
while (1) {
|
||||
// gobble whitespace
|
||||
while (*buf && strchr(WHITESPACE, *buf))
|
||||
*buf++ = 0;
|
||||
if (*buf == 0)
|
||||
break;
|
||||
|
||||
// save and scan past next arg
|
||||
if (argc == MAXARGS-1) {
|
||||
cprintf("Too many arguments (max %d)\n", MAXARGS);
|
||||
return 0;
|
||||
}
|
||||
argv[argc++] = buf;
|
||||
while (*buf && !strchr(WHITESPACE, *buf))
|
||||
buf++;
|
||||
}
|
||||
argv[argc] = 0;
|
||||
|
||||
// Lookup and invoke the command
|
||||
if (argc == 0)
|
||||
return 0;
|
||||
for (i = 0; i < ARRAY_SIZE(commands); i++) {
|
||||
if (strcmp(argv[0], commands[i].name) == 0)
|
||||
return commands[i].func(argc, argv, tf);
|
||||
}
|
||||
cprintf("Unknown command '%s'\n", argv[0]);
|
||||
return 0;
|
||||
}
|
||||
|
||||
void
|
||||
monitor(struct Trapframe *tf)
|
||||
{
|
||||
char *buf;
|
||||
|
||||
cprintf("Welcome to the JOS kernel monitor!\n");
|
||||
cprintf("Type 'help' for a list of commands.\n");
|
||||
|
||||
|
||||
while (1) {
|
||||
buf = readline("K> ");
|
||||
if (buf != NULL)
|
||||
if (runcmd(buf, tf) < 0)
|
||||
break;
|
||||
}
|
||||
}
|
||||
19
kern/monitor.h
Normal file
19
kern/monitor.h
Normal file
@@ -0,0 +1,19 @@
|
||||
#ifndef JOS_KERN_MONITOR_H
|
||||
#define JOS_KERN_MONITOR_H
|
||||
#ifndef JOS_KERNEL
|
||||
# error "This is a JOS kernel header; user programs should not #include it"
|
||||
#endif
|
||||
|
||||
struct Trapframe;
|
||||
|
||||
// Activate the kernel monitor,
|
||||
// optionally providing a trap frame indicating the current state
|
||||
// (NULL if none).
|
||||
void monitor(struct Trapframe *tf);
|
||||
|
||||
// Functions implementing monitor commands.
|
||||
int mon_help(int argc, char **argv, struct Trapframe *tf);
|
||||
int mon_kerninfo(int argc, char **argv, struct Trapframe *tf);
|
||||
int mon_backtrace(int argc, char **argv, struct Trapframe *tf);
|
||||
|
||||
#endif // !JOS_KERN_MONITOR_H
|
||||
37
kern/printf.c
Normal file
37
kern/printf.c
Normal file
@@ -0,0 +1,37 @@
|
||||
// Simple implementation of cprintf console output for the kernel,
|
||||
// based on printfmt() and the kernel console's cputchar().
|
||||
|
||||
#include <inc/types.h>
|
||||
#include <inc/stdio.h>
|
||||
#include <inc/stdarg.h>
|
||||
|
||||
|
||||
static void
|
||||
putch(int ch, int *cnt)
|
||||
{
|
||||
cputchar(ch);
|
||||
*cnt++;
|
||||
}
|
||||
|
||||
int
|
||||
vcprintf(const char *fmt, va_list ap)
|
||||
{
|
||||
int cnt = 0;
|
||||
|
||||
vprintfmt((void*)putch, &cnt, fmt, ap);
|
||||
return cnt;
|
||||
}
|
||||
|
||||
int
|
||||
cprintf(const char *fmt, ...)
|
||||
{
|
||||
va_list ap;
|
||||
int cnt;
|
||||
|
||||
va_start(ap, fmt);
|
||||
cnt = vcprintf(fmt, ap);
|
||||
va_end(ap);
|
||||
|
||||
return cnt;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user