This commit is contained in:
Anish Athalye
2018-09-25 12:22:51 -04:00
parent 2d1187aa3c
commit a9d7717cc4
44 changed files with 1899 additions and 8 deletions

16
user/Makefrag Normal file
View File

@@ -0,0 +1,16 @@
OBJDIRS += user
USERLIBS += jos
$(OBJDIR)/user/%.o: user/%.c $(OBJDIR)/.vars.USER_CFLAGS
@echo + cc[USER] $<
@mkdir -p $(@D)
$(V)$(CC) -nostdinc $(USER_CFLAGS) -c -o $@ $<
$(OBJDIR)/user/%: $(OBJDIR)/user/%.o $(OBJDIR)/lib/entry.o $(USERLIBS:%=$(OBJDIR)/lib/lib%.a) user/user.ld
@echo + ld $@
$(V)$(LD) -o $@ $(ULDFLAGS) $(LDFLAGS) -nostdlib $(OBJDIR)/lib/entry.o $@.o -L$(OBJDIR)/lib $(USERLIBS:%=-l%) $(GCC_LIB)
$(V)$(OBJDUMP) -S $@ > $@.asm
$(V)$(NM) -n $@ > $@.sym

11
user/badsegment.c Normal file
View File

@@ -0,0 +1,11 @@
// program to cause a general protection exception
#include <inc/lib.h>
void
umain(int argc, char **argv)
{
// Try to load the kernel's TSS selector into the DS register.
asm volatile("movw $0x28,%ax; movw %ax,%ds");
}

10
user/breakpoint.c Normal file
View File

@@ -0,0 +1,10 @@
// program to cause a breakpoint trap
#include <inc/lib.h>
void
umain(int argc, char **argv)
{
asm volatile("int $3");
}

11
user/buggyhello.c Normal file
View File

@@ -0,0 +1,11 @@
// buggy hello world -- unmapped pointer passed to kernel
// kernel should destroy user environment in response
#include <inc/lib.h>
void
umain(int argc, char **argv)
{
sys_cputs((char*)1, 1);
}

13
user/buggyhello2.c Normal file
View File

@@ -0,0 +1,13 @@
// buggy hello world 2 -- pointed-to region extends into unmapped memory
// kernel should destroy user environment in response
#include <inc/lib.h>
const char *hello = "hello, world\n";
void
umain(int argc, char **argv)
{
sys_cputs(hello, 1024*1024);
}

13
user/divzero.c Normal file
View File

@@ -0,0 +1,13 @@
// buggy program - causes a divide by zero exception
#include <inc/lib.h>
int zero;
void
umain(int argc, char **argv)
{
zero = 0;
cprintf("1/0 is %08x!\n", 1/zero);
}

12
user/evilhello.c Normal file
View File

@@ -0,0 +1,12 @@
// evil hello world -- kernel pointer passed to kernel
// kernel should destroy user environment in response
#include <inc/lib.h>
void
umain(int argc, char **argv)
{
// try to print the kernel entry point as a string! mua ha ha!
sys_cputs((char*)0xf010000c, 100);
}

10
user/faultread.c Normal file
View File

@@ -0,0 +1,10 @@
// buggy program - faults with a read from location zero
#include <inc/lib.h>
void
umain(int argc, char **argv)
{
cprintf("I read %08x from location 0!\n", *(unsigned*)0);
}

10
user/faultreadkernel.c Normal file
View File

@@ -0,0 +1,10 @@
// buggy program - faults with a read from kernel space
#include <inc/lib.h>
void
umain(int argc, char **argv)
{
cprintf("I read %08x from location 0xf0100000!\n", *(unsigned*)0xf0100000);
}

10
user/faultwrite.c Normal file
View File

@@ -0,0 +1,10 @@
// buggy program - faults with a write to location zero
#include <inc/lib.h>
void
umain(int argc, char **argv)
{
*(unsigned*)0 = 0;
}

10
user/faultwritekernel.c Normal file
View File

@@ -0,0 +1,10 @@
// buggy program - faults with a write to a kernel location
#include <inc/lib.h>
void
umain(int argc, char **argv)
{
*(unsigned*)0xf0100000 = 0;
}

9
user/hello.c Normal file
View File

@@ -0,0 +1,9 @@
// hello, world
#include <inc/lib.h>
void
umain(int argc, char **argv)
{
cprintf("hello, world\n");
cprintf("i am environment %08x\n", thisenv->env_id);
}

10
user/softint.c Normal file
View File

@@ -0,0 +1,10 @@
// buggy program - causes an illegal software interrupt
#include <inc/lib.h>
void
umain(int argc, char **argv)
{
asm volatile("int $14"); // page fault
}

27
user/testbss.c Normal file
View File

@@ -0,0 +1,27 @@
// test reads and writes to a large bss
#include <inc/lib.h>
#define ARRAYSIZE (1024*1024)
uint32_t bigarray[ARRAYSIZE];
void
umain(int argc, char **argv)
{
int i;
cprintf("Making sure bss works right...\n");
for (i = 0; i < ARRAYSIZE; i++)
if (bigarray[i] != 0)
panic("bigarray[%d] isn't cleared!\n", i);
for (i = 0; i < ARRAYSIZE; i++)
bigarray[i] = i;
for (i = 0; i < ARRAYSIZE; i++)
if (bigarray[i] != i)
panic("bigarray[%d] didn't hold its value!\n", i);
cprintf("Yes, good. Now doing a wild write off the end...\n");
bigarray[ARRAYSIZE+1024] = 0;
panic("SHOULD HAVE TRAPPED!!!");
}

72
user/user.ld Normal file
View File

@@ -0,0 +1,72 @@
/* Simple linker script for JOS user-level programs.
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
{
/* Load programs at this address: "." means the current address */
. = 0x800020;
.text : {
*(.text .stub .text.* .gnu.linkonce.t.*)
}
PROVIDE(etext = .); /* Define the 'etext' symbol to this value */
.rodata : {
*(.rodata .rodata.* .gnu.linkonce.r.*)
}
/* Adjust the address for the data segment to the next page */
. = ALIGN(0x1000);
.data : {
*(.data)
}
PROVIDE(edata = .);
.bss : {
*(.bss)
}
PROVIDE(end = .);
/* Place debugging symbols so that they can be found by
* the kernel debugger.
* Specifically, the four words at 0x200000 mark the beginning of
* the stabs, the end of the stabs, the beginning of the stabs
* string table, and the end of the stabs string table, respectively.
*/
.stab_info 0x200000 : {
LONG(__STAB_BEGIN__);
LONG(__STAB_END__);
LONG(__STABSTR_BEGIN__);
LONG(__STABSTR_END__);
}
.stab : {
__STAB_BEGIN__ = DEFINED(__STAB_BEGIN__) ? __STAB_BEGIN__ : .;
*(.stab);
__STAB_END__ = DEFINED(__STAB_END__) ? __STAB_END__ : .;
BYTE(0) /* Force the linker to allocate space
for this section */
}
.stabstr : {
__STABSTR_BEGIN__ = DEFINED(__STABSTR_BEGIN__) ? __STABSTR_BEGIN__ : .;
*(.stabstr);
__STABSTR_END__ = DEFINED(__STABSTR_END__) ? __STABSTR_END__ : .;
BYTE(0) /* Force the linker to allocate space
for this section */
}
/DISCARD/ : {
*(.eh_frame .note.GNU-stack .comment)
}
}