├── LICENSE ├── Makefile ├── README ├── _curs ├── bin ├── execvpe │ └── execvpe.c ├── fork │ └── fork.c ├── fs │ └── fs.c ├── hello │ └── hello.c ├── hello1 │ └── hello1.c ├── ls │ └── ls.c ├── malloc │ └── malloc.c ├── sh │ └── sh.c └── test │ └── test.c ├── core ├── crt └── crt1.c ├── cscope.out ├── cse506-pubkey.txt ├── file ├── fork ├── fs.c.bac ├── go_gdb ├── hdd.img ├── include ├── .fs.h.swp ├── assert.h ├── define.h ├── defs.h ├── env.h ├── error.h ├── fs.h ├── keyboard.h ├── memlayout.h ├── mmu.h ├── ports.h ├── print.h ├── process.h ├── stdio.h ├── stdlib.h ├── string.h ├── sys │ ├── ahci.h │ ├── env.h │ ├── fs.h │ ├── gdt.h │ ├── idt.h │ ├── isr.h │ ├── kbc.h │ ├── kbc_scancodes.h │ ├── memlayout.h │ ├── mmu.h │ ├── pmap.h │ ├── ports.h │ ├── print.h │ ├── process.h │ ├── tarfs.h │ ├── timer.h │ └── vmem.h ├── syscall.h └── utils.h ├── initiate_superblock ├── initiate_superblock.c ├── kernel ├── ld └── ld.c ├── libc ├── execvpe.c ├── exit.c ├── fork.c ├── getpid.c ├── getppid.c ├── idt.c ├── idt.s ├── isr.c ├── isr.s ├── keyboard.c ├── malloc.c ├── open.c ├── opendir.c ├── ports.c ├── print.c ├── printf.c ├── printf.c.bak ├── printf_color.c ├── ps.c ├── read.c ├── readdir.c ├── scanf.c ├── seek.c ├── shell_sleep.c ├── shutdown.c ├── sleep.c ├── string.c ├── syscall.c ├── timer.c ├── waitpid.c └── write.c ├── linker.script ├── obj ├── bin │ ├── hello │ │ ├── hello.o │ │ └── hello1.o │ ├── hello1 │ │ └── hello1.o │ └── test │ │ └── test.o ├── crt │ └── crt1.o ├── ld │ └── ld.o ├── libc │ ├── exit.o │ ├── idt.o │ ├── isr.o │ ├── keyboard.o │ ├── ports.o │ ├── print.o │ ├── printf.o │ ├── string.o │ ├── syscall.o │ └── timer.o ├── sys │ ├── env.o │ ├── fs.o │ ├── gdt.asm.o │ ├── gdt.o │ ├── idt.asm.o │ ├── idt.o │ ├── isr.asm.o │ ├── isr.o │ ├── keyboard.o │ ├── main.o │ ├── pmap.o │ ├── ports.o │ ├── print.o │ ├── process.asm.o │ ├── process.o │ ├── string.o │ ├── tarfs.o │ ├── timer.o │ ├── utils.o │ └── vmem.o └── tarfs.o ├── qemu ├── qemu.log ├── qemud ├── qemuhdd ├── rajesh.img ├── rajesh.iso ├── rgolani.img ├── rgolani.iso ├── rgolani.tgz ├── rgolani.tgz.gpg ├── rootfs ├── aaatest ├── bin │ ├── .empty │ ├── hello │ ├── hello1 │ └── test ├── boot │ ├── beastie.4th │ ├── boot │ ├── boot0 │ ├── boot1 │ ├── boot2 │ ├── cdboot │ ├── defaults │ │ └── loader.conf │ ├── gptboot │ ├── kernel │ │ ├── .empty │ │ └── kernel │ ├── loader │ ├── loader.4th │ ├── loader.conf │ ├── loader.rc │ ├── mbr │ ├── pxeboot │ ├── screen.4th │ └── support.4th ├── etc │ ├── .empty │ └── dummy ├── lib │ ├── .empty │ ├── crt1.o │ ├── ld.so │ ├── libc.a │ └── libc.so ├── shcommands ├── tease └── test ├── superblock ├── sys ├── ahci.c ├── assert.h ├── env.c ├── fs.c ├── gdt.c ├── gdt.s ├── idt.c ├── idt.s ├── isr.c ├── isr.s ├── kbc.c ├── keyboard.c ├── main.c ├── pmap.c ├── ports.c ├── print.c ├── process.c ├── process.s ├── string.c ├── tarfs.c ├── timer.c ├── utils.c ├── vmem.c └── vmem.s ├── tags ├── tarfs └── upload ├── assert.h ├── memlayout.h ├── mmu.h ├── pmap.c ├── pmap.h └── types.h /Makefile: -------------------------------------------------------------------------------- 1 | 2 | CC=gcc 3 | AS=as 4 | CFLAGS=-O1 -Wall -Werror -nostdinc -Iinclude -msoft-float -mno-sse -mno-red-zone -fno-builtin -fPIC -mtune=amdfam10 -g3 5 | LD=ld 6 | LDLAGS=-nostdlib 7 | AR=ar 8 | 9 | ROOTFS=rootfs 10 | ROOTBIN=$(ROOTFS)/bin 11 | ROOTLIB=$(ROOTFS)/lib 12 | ROOTBOOT=$(ROOTFS)/boot 13 | 14 | KERN_SRCS:=$(wildcard sys/*.c sys/*.s sys/*/*.c sys/*/*.s) 15 | BIN_SRCS:=$(wildcard bin/*/*.c) 16 | LIBC_SRCS:=$(wildcard libc/*.c libc/*/*.c) 17 | LD_SRCS:=$(wildcard ld/*.c) 18 | BINS:=$(addprefix $(ROOTFS)/,$(wildcard bin/*)) 19 | 20 | .PHONY: all binary 21 | 22 | all: $(USER).iso $(USER).img 23 | 24 | $(USER).iso: kernel 25 | cp kernel $(ROOTBOOT)/kernel/kernel 26 | mkisofs -r -no-emul-boot -input-charset utf-8 -b boot/cdboot -o $@ $(ROOTFS)/ 27 | 28 | newfs.506: $(wildcard newfs/*.c) 29 | $(CC) -o $@ $^ 30 | 31 | kernel: $(patsubst %.s,obj/%.asm.o,$(KERN_SRCS:%.c=obj/%.o)) obj/tarfs.o 32 | $(LD) $(LDLAGS) -o $@ -T linker.script $^ 33 | 34 | obj/tarfs.o: $(BINS) 35 | tar --format=ustar -cvf tarfs --no-recursion -C $(ROOTFS) $(shell find $(ROOTFS)/ -name boot -prune -o ! -name .empty -printf "%P\n") 36 | objcopy --input binary --binary-architecture i386 --output elf64-x86-64 tarfs $@ 37 | 38 | $(ROOTLIB)/libc.a: $(LIBC_SRCS:%.c=obj/%.o) 39 | $(AR) rcs $@ $^ 40 | 41 | $(ROOTLIB)/libc.so: $(LIBC_SRCS:%.c=obj/%.o) $(ROOTLIB)/ld.so 42 | $(LD) $(LDLAGS) -shared -soname=$@ --dynamic-linker=/lib/ld.so --rpath-link=/lib -o $@ $^ 43 | 44 | $(ROOTLIB)/ld.so: $(LD_SRCS:%.c=obj/%.o) 45 | $(LD) $(LDLAGS) -shared -o $@ $^ 46 | 47 | $(ROOTLIB)/crt1.o: obj/crt/crt1.o 48 | cp $^ $@ 49 | 50 | $(BINS): $(ROOTLIB)/crt1.o $(ROOTLIB)/libc.a $(ROOTLIB)/libc.so $(shell find bin/ -type f -name *.c) 51 | @$(MAKE) --no-print-directory BIN=$@ binary 52 | 53 | binary: $(patsubst %.c,obj/%.o,$(wildcard $(BIN:rootfs/%=%)/*.c)) 54 | $(LD) $(LDLAGS) -o $(BIN) $(ROOTLIB)/crt1.o $^ $(ROOTLIB)/libc.a 55 | 56 | obj/%.o: %.c $(wildcard include/*.h include/*/*.h) 57 | @mkdir -p $(dir $@) 58 | $(CC) -c $(CFLAGS) -o $@ $< 59 | 60 | obj/%.asm.o: %.s 61 | @mkdir -p $(dir $@) 62 | $(AS) -o $@ $< 63 | 64 | .PHONY: submit clean 65 | 66 | SUBMITTO:=~mferdman/cse506-submit/ 67 | 68 | submit: clean 69 | tar -czvf $(USER).tgz --exclude=.empty --exclude=.*.sw? --exclude=*~ LICENSE README Makefile linker.script sys bin crt libc ld include $(ROOTFS) $(USER).img hdd.img initiate_superblock initiate_superblock.c superblock 70 | @gpg --quiet --import cse506-pubkey.txt 71 | gpg --yes --encrypt --recipient 'CSE506' $(USER).tgz 72 | rm -fv $(SUBMITTO)$(USER)=*.tgz.gpg 73 | cp -v $(USER).tgz.gpg $(SUBMITTO)$(USER)=`date +%F=%T`.tgz.gpg 74 | 75 | clean: 76 | find $(ROOTLIB) $(ROOTBIN) -type f ! -name .empty -print -delete 77 | rm -rfv obj kernel newfs.506 $(ROOTBOOT)/kernel/kernel 78 | -------------------------------------------------------------------------------- /README: -------------------------------------------------------------------------------- 1 | =====70 POINT PROJECT===== 2 | 3 | ==USEFUL TIPS== 4 | 5 | For filesystem, use the disk named "hdd.img" in cse506 folder. Disk Image size is 20 MB 6 | 7 | If new disk is to created, here are the following steps: 8 | 9 | 1. Run the qemu command to create a 1 MB disk named "hdd.img" : qemu-img create hdd.img 20M 10 | 11 | 2. Insert superblock to that image using command : dd if=superblock of=hdd.img seek=1 conv=notrunc (superblock file is provided in the home directory, which is being generated using initialize_superblock.c) 12 | 13 | 3. Now, the hdd.img is ready to be used by our OS. Run Qemu with following command : qemu-system-x86_64 -curses -cdrom $USER.iso -drive id=disk,file=hdd.img,if=none -device ahci,id=ahci -device ide-drive,drive=disk,bus=ahci.0 -gdb tcp::$1 14 | 15 | 16 | For Shutdown : type shutdown on shell. 17 | 18 | For any queries, please contact SBUnix developers. 19 | 20 | ==SYSTEM CALLS== 21 | /* File System Commands */ 22 | READ() 23 | Syntax : read(file *file, uint64_t addr, char *buf ) 24 | Description : Whenever called this function reads the file in to the buffer 'buf'. 25 | Dependencies : To open a file always use OPEN system call before reading and after the read is completed, close the file. This call returns -1 for the files whose WRONLY flag is set. 26 | return : This call returns the length of bytes read into the buffer or -1 if there is an error 27 | 28 | 29 | WRITE() 30 | Syntax : int write(file *fd, char * buf, int size) 31 | Description : Whenever called it writes up to 'size' number of bytes into the file descriptor 'fd' from the buffer starting at 'addr'. 32 | Dependencies : To open a file always use OPEN system call before reading and after the read is completed, close the file. 33 | This call returns -1 for the files opened with O_RDONLY flag set. It cannot do any write operations on tarfs. 34 | return : This function retruns length of bytes written into the buffer or -1 on error 35 | 36 | MALLOC() 37 | Syntax : void *malloc(uint64_t size) 38 | Description : Whenever called the size of vma is increased by 'size'. 39 | return : This call returns the virtual address in heap 40 | 41 | FORK() 42 | Syntax : pid_t fork (void); 43 | Description : Whenever called a child process is created. It returns pid = 0 for child process and non zero for parent process. 44 | return : This call returns the pid for parent process or 0 for the child process. 45 | 46 | EXECVPE() 47 | Syntax : execvpe(char *file, char *argv[], char *envp[]) 48 | Description : Whenever called a file along with any necessary arguments argv[] in the environment envp[] is executed. 49 | return : This call returns -1 on failure, and doesnot return anything on success 50 | 51 | WAIT() 52 | Syntax : wait(uint64_t status) 53 | Description : Whenever called, the parent waits for a child process to exit. 54 | return : This call returns -1 if the parent has no children. 55 | 56 | EXIT() 57 | Syntax : exit(int status) 58 | Description : Whenever called the calling process is terminated immediately. 59 | 60 | GETPID() 61 | Syntax : getpid() 62 | Description : Whenever called the process id of currently running process is returned. 63 | 64 | GETPPID() 65 | Syntax : getppid() 66 | Description : Whenever called the parent's process id of currently running process is returned. 67 | 68 | PS() 69 | Syntax : ps() 70 | Description : Whenever called the list of all currently running processes is returned. 71 | 72 | OPENDIR() 73 | Syntax : opendir(uint64_t* entry, uint64_t* directory) 74 | Description : Whenever called,a directory struct with list of all inode/files in it is returned. 75 | return : If an error occurs it will return directory struct with the values set to NULL. 76 | 77 | READDIR() 78 | Syntax : readdir(DIR* node) 79 | Description : Whenver called iteratively, it will list contents in directory. 80 | Dependencies : Read directory system call is always followed after open directory system call. 81 | return : It will return the pointer to dirent structure .If end of directory is reached or an error occurs then it returns NULL. 82 | 83 | OPEN() 84 | Syntax : open(char* dir_path, uint64_t flags) 85 | Description : Whenever called, a file is opened and a file descriptor is returned. It opens files from 'tarfs' and disk depending on 'dir_path'. 86 | Dependencies : The flags can be one of the following: 'O_CREAT', 'O_RDONLY', 'O_WRONLY', 'O_APPEND', 'O_TRUNC', 'O_RDWR'. 87 | return : This call returns file descriptor number open on a directory returns -1 as file descriptor. 88 | 89 | SLEEP() 90 | Syntax : sleep(int msec) 91 | Description : The state of the current task is changed to 'sleep' for 'msec' milliseconds. 92 | 93 | CLEAR() 94 | Syntax : clear() 95 | Description : Clears the screen. 96 | 97 | SEEK() 98 | Syntax : seek(uint64_t fd_type, int offset, int whence) 99 | Description : When used it will move the current file pointer by offset number of bytes that is in the file. 100 | Dependencies : whence can be SEEK_SET, SEEK_CUR, SEEK_END. Using this will provide the offset from start , current pointer or end of file. 101 | 102 | SHUTDOWN() 103 | Syntax : shutdown() 104 | Description : This function is used to exit from all the processes. 105 | 106 | -------------------------------------------------------------------------------- /bin/execvpe/execvpe.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | int main(int argc, char* argv[], char* envp[]) { 5 | uint64_t *test = (uint64_t *)malloc(sizeof(uint64_t)); 6 | *test = 1; 7 | 8 | char *a[]={"-a","-b","-c"}; 9 | char *b[]={"dummy","value","pass"}; 10 | int k = fork(); 11 | if(k == 0) 12 | execvpe("bin/test",a,b); 13 | else 14 | printf("\nVoldemort, My child has been born. He'll have his vengence."); 15 | exit(-1); 16 | return 0; 17 | } 18 | 19 | -------------------------------------------------------------------------------- /bin/fork/fork.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | int j; 5 | int main(int argc, char* argv[], char* envp[]) { 6 | j = fork(); 7 | //List all processes in ready queue 8 | ps(); 9 | if(j==0) { 10 | printf("I am a child of %d with id %d\n", getppid(), getpid()); 11 | 12 | } 13 | else { 14 | printf("I am a parent with id %d\n" , getpid()); 15 | 16 | } 17 | exit(1); 18 | return 0; 19 | } 20 | -------------------------------------------------------------------------------- /bin/fs/fs.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | int main(int argc, char* argv[], char* envp[]) { 5 | char buf[1024] = "CSE 506 was an amazing course. Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book. It has survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged. It was popularised in the 1960s with the release of Letraset sheets containing Lorem Ipsum passages, and more recently with desktop publishing software like Aldus PageMaker including versions of Lorem Ipsum."; 6 | printf(" File contents are : %s", buf); 7 | 8 | //File write 9 | file *fd = open("hdd/cse506.txt"); 10 | write(fd, buf, 30); 11 | 12 | char test[100]; 13 | 14 | seek(fd, 100 , SEEK_SET); 15 | read(fd, 100, test); 16 | printf(" \n Reading after seeking 100 bytes from start : %s \n",test); 17 | 18 | seek(fd, 100 , SEEK_CUR); 19 | read(fd, 100, test); 20 | printf(" \n Reading after seeking 100 bytes from end : %s \n",test); 21 | 22 | 23 | exit(2); 24 | return 0; 25 | } 26 | -------------------------------------------------------------------------------- /bin/hello/hello.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | int j; 5 | int main(int argc, char* argv[], char* envp[]) { 6 | // uint64_t *mem; 7 | //printf("\nP1"); 8 | // j = fork(); 9 | char buf[1024] = "asdsd assda sasads assadsad"; 10 | printf("%s", buf); 11 | 12 | file *fd = open("hdd/test1234.txt"); 13 | write(fd, buf, 30); 14 | //int size = open("hdd/test3.txt"), 32, (uint64_t) buf); 15 | printf("\nSize of %s", fd->filename); 16 | //printf("\n %s", buf); 17 | //seek(fd, 900, SEEK_END); 18 | //seek(fd, 100, SEEK_CUR); 19 | //seek(fd, 512, SEEK_SET); 20 | char* test = (char *)malloc(1000*sizeof(char)); 21 | read(fd, 100, test); 22 | printf(" \n offset %d %s \n",fd->offset, test); 23 | 24 | //while(1) 25 | /*uint64_t add = opendir("bin/"); 26 | printf("%x", add); 27 | readdir(add); 28 | */ 29 | /*int i = 0 ; 30 | printf("enter no "); 31 | scanf("%d", &i); 32 | printf("%d", i); 33 | */ 34 | /*char test[10]; 35 | printf("\n %x", test); 36 | printf("enter string"); 37 | scanf("%s", test); 38 | printf("%s", test); 39 | */ 40 | // printf("[[[[[[[[[[[[]]]]]]]]]]]]value of j==%d[[[[[[[[[[[[[[[[]]]]]]]]]]]]]]]]",j); 41 | //printf("[[[[[[[[[[[[]]]]]]]]]]]]value of j==%d[[[[[[[[[[[[[[[[]]]]]]]]]]]]]]]]",j); 42 | /* if(j==0) { 43 | while(1) 44 | ps(); 45 | // printf("I am a child of %d with id %d\n", getppid(), getpid()); 46 | 47 | } 48 | else { 49 | //printf("[[[[[[[[[[[[]]]]]]]]]]]]value of j==%d[[[[[[[[[[[[[[[[]]]]]]]]]]]]]]]]",j); 50 | while(1) 51 | readdir("bin/"); 52 | //ps(); 53 | //printf("\nparent"); 54 | printf("I am a parent with id %d\n" , getpid()); 55 | 56 | } 57 | */ 58 | // mem=malloc(512); 59 | // printf("\nmem=[%x]\n",mem); 60 | while(1){ 61 | // printf("P1"); 62 | }; 63 | // exit(1); 64 | return 0; 65 | } 66 | -------------------------------------------------------------------------------- /bin/hello1/hello1.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | int main(int argc, char* argv[], char* envp[]) { 5 | //printf("\nProcess[%d], name[%s], [%x]\n", 2, "P2", 8765); 6 | //printf("P2"); 7 | uint64_t *test = (uint64_t *)malloc(sizeof(uint64_t)); 8 | *test = 1; 9 | // while(1){ 10 | // int i = 0; 11 | // sleep(20); 12 | 13 | char *a[]={"-a","-b","-c"}; 14 | char *b[]={"dummy","value","pass"}; 15 | execvpe("bin/test",a,b); 16 | //for(i=1; i<200; i++) 17 | while(1) 18 | printf("\n P2 >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>"); 19 | // } 20 | 21 | exit(-1); 22 | return 0; 23 | } 24 | -------------------------------------------------------------------------------- /bin/ls/ls.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | int main(int argc, char* argv[], char* envp[]) { 5 | 6 | printf("\n\ninside ls %x", argv[0]); 7 | 8 | exit(-1); 9 | return 0; 10 | } 11 | -------------------------------------------------------------------------------- /bin/malloc/malloc.c: -------------------------------------------------------------------------------- 1 | 2 | #include 3 | #include 4 | #include 5 | int main(int argc, char* argv[], char* envp[]) { 6 | 7 | char *str = (char *)malloc(10*sizeof(char)); 8 | 9 | printf("Enter string : "); 10 | scanf("%s", str); 11 | printf("\n String is %s", str); 12 | exit(1); 13 | return 0; 14 | } 15 | 16 | -------------------------------------------------------------------------------- /bin/test/test.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | int main(int argc, char* argv[], char* envp[]) { 5 | int i; 6 | for(i = 0; i< 5; i++) 7 | printf("Execvped process. Before Sleep. \n"); 8 | 9 | sleep(5); 10 | 11 | for(i = 0; i< 5; i++) 12 | printf(" Execvped process. After Sleep.\n"); 13 | 14 | exit(-1); 15 | return 0; 16 | } 17 | -------------------------------------------------------------------------------- /core: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/rajesh5310/SBUnix/441268cc45d5f133ca9cbe9566d462320303d86e/core -------------------------------------------------------------------------------- /crt/crt1.c: -------------------------------------------------------------------------------- 1 | #include 2 | void _start(void) { 3 | int argc = 1; 4 | char* argv[10]; 5 | char* envp[10]; 6 | int res; 7 | res = main(argc, argv, envp); 8 | exit(res); 9 | } 10 | -------------------------------------------------------------------------------- /cscope.out: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/rajesh5310/SBUnix/441268cc45d5f133ca9cbe9566d462320303d86e/cscope.out -------------------------------------------------------------------------------- /cse506-pubkey.txt: -------------------------------------------------------------------------------- 1 | -----BEGIN PGP PUBLIC KEY BLOCK----- 2 | Version: GnuPG v1.4.11 (GNU/Linux) 3 | 4 | mQENBFIP0i0BCADDz3pDG65+bTr5TXDnayacSECaqT/PWmrDH4EjJSrh11chgQtR 5 | bJCFmg4r7ruyeMlKsxXdcO5iFDO8DaIa/IAcGHDTKdIIw+uvl055UAaLkDWu95SB 6 | SWt42DI7Mif+r85w15t6Avp6sC+djD94qYSx1RRHg9IRH1MUvIMVRqbcOpKHgIGD 7 | 1TI1Rp+Qj8z7T+AeJ+eaWW8gYH7F/HxMPBisZxVY0oRaCV69ThyRMzxEfFKPJVKG 8 | OKoBvYLT994eXx/g3D3jtsyT4pmWgWD/CB4soIkuwGFdwSWWGVJTcAdmbSCdb6/4 9 | 6QXWeN6jPJCConAKQpW9zAB84OP08OHGJAXnABEBAAG0Mk1pa2UgRmVyZG1hbiAo 10 | Q1NFNTA2KSA8bWZlcmRtYW5AY3Muc3Rvbnlicm9vay5lZHU+iQE4BBMBAgAiBQJS 11 | D9ItAhsDBgsJCAcDAgYVCAIJCgsEFgIDAQIeAQIXgAAKCRD49GLEehrSankAB/9B 12 | 0ezY6wWO+IRfO34/ERzM/JpUjAoK1UQlNjOHanxSqk9cUszubJuZ2m3WQ5soQscT 13 | 6X9yEcc3MbWPrb9XSGXMjbUaKMqlyRV0htWC2j9MMbpo09hUWvy0KWFGIUaEb8DG 14 | KXpvAmUOL0GWWeysRz292PbfInz2iz7GHvvIQo6QP8jgWxu9upskWmuo1gNtq8+c 15 | ypv5ASUNBvvitHNZuUbYQbO7nTb7br9brZ48XWGiymXY+WNvlQgvECWFJtI1Fesm 16 | 9/Mnl3gzPkwk5Nips/6zpNwtzVmt1fdE4n967ei/XV/u/ZWS0G4TnR7C/CfsBeie 17 | pOTdmJ9HEGO2UdKXIGNluQENBFIP0i0BCADyGTZGCjJdc0fCCyXWJX6om6k2cBRF 18 | /GXyGHCxbaclX4o8/vDrBUFnQPYQr6DjdBX7XcyH1kuYCt+trnueylJxiCGAaOZ7 19 | NFr6b6TOJm+lyxlhbwRVM6gyZqhVEcy9k/hj8k7TLPPWWWItFGqN32RlZfEsbQu1 20 | kZgEQyQ5Czpp7lnkK6+WfGanzZ6fRcZVyaHK9iT/JrvBsyF7ziy0gKt6/G+czq2F 21 | l3dy7Y8jjg1g1J7V0NBIHkQ0EX3SbvW7W6u6l4xkDvNOaJqlQvJ7Ot4ErhtMO+7j 22 | JzkhdSI1T6/8rKnZ5KrGaOvWLN8FP2ZRpTxYUOW0R2V1MlQCgBNqS9ltABEBAAGJ 23 | AR8EGAECAAkFAlIP0i0CGwwACgkQ+PRixHoa0mqHMQf/f3ocyhL8Dk8EJSbO7Ghz 24 | 9toyG8Q5BtXjiSq3k7mUzkvU0eQwAdsEnAxcmIwSz2TDQQSLXAi3JjdbkUtqXv9l 25 | neYHsHVh3da+CepZ/Pd05X7Q6qfmkW5P40o5obaApKsJJdrI7JFGT79bo//wQZlB 26 | 9RdH9CCJaU46iBhyC3Iw40SDXvG7vK1/BfgLbwmnQZwi596JKbD/LFBprvb9ARg6 27 | ixS+l0SXt412uMg6aZDFFRCksVhnuXr9QrgWLYsAO7xbErLuYytfRdWRv3UzRfjY 28 | I3AEfEH9d4z4blTPSNQTLf1Ra/KVXLK8ujsugasOgRPECF+mbc/5e45zLFpA7//x 29 | KA== 30 | =fA6U 31 | -----END PGP PUBLIC KEY BLOCK----- 32 | -------------------------------------------------------------------------------- /go_gdb: -------------------------------------------------------------------------------- 1 | gdb ./kernel 2 | target remote localhost:1234 3 | -------------------------------------------------------------------------------- /hdd.img: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/rajesh5310/SBUnix/441268cc45d5f133ca9cbe9566d462320303d86e/hdd.img -------------------------------------------------------------------------------- /include/.fs.h.swp: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/rajesh5310/SBUnix/441268cc45d5f133ca9cbe9566d462320303d86e/include/.fs.h.swp -------------------------------------------------------------------------------- /include/assert.h: -------------------------------------------------------------------------------- 1 | #ifndef ASSERT_H 2 | #define ASSERT_H 3 | 4 | #include 5 | 6 | #define assert(x) \ 7 | do { if (!(x)) print("assertion failed: %s", #x); } while (0) 8 | 9 | // static_assert(x) will generate a compile-time error if 'x' is false. 10 | #define static_assert(x) switch (x) case 0: case (x): 11 | 12 | #endif /* ASSERT_H */ 13 | -------------------------------------------------------------------------------- /include/define.h: -------------------------------------------------------------------------------- 1 | #ifndef _DEFINE_H 2 | #define _DEFINE_H 3 | //Pagging 4 | #define NUMBER_OF_PAGES 32768 5 | #define PAGE_SIZE 4096 6 | #define PAGE_SIZE_HEX 0x1000 7 | #define BIT_SIZE 8 8 | #define AVAILABLE 0 9 | #define VIRTUAL_KERNEL_BASE 0xFFFFFFFF80200000 10 | #define VIRTUAL_VIDEO_MEM_START 0xFFFFFFFF800B8000 11 | #define VIDEO_MEM_START 0xB8000 12 | #define VIDEO_MEM_END 0xB89000 13 | #define FLAGS_SUP_OFF 0x003 14 | #define FLAGS_SUP_ON 0x003 15 | #define PAGE_ALIGN_BITS 0xFFFFFFF0 //Important Required because of problem wih 7 byte offset 16 | #define PAGE_ALIGN 0xFFFFFFFFFFFFF000 17 | #define NOT_AVAILABLE 0x0 18 | #endif 19 | -------------------------------------------------------------------------------- /include/defs.h: -------------------------------------------------------------------------------- 1 | #ifndef DEFS_H 2 | #define DEFS_H 3 | 4 | #ifndef NULL 5 | #define NULL ((void*) 0) 6 | #endif 7 | 8 | // Represents true-or-false values 9 | typedef _Bool bool; 10 | enum { false, true }; 11 | 12 | // Explicitly-sized versions of integer types 13 | 14 | typedef __signed char int8_t; 15 | typedef unsigned char uint8_t; 16 | 17 | typedef short int16_t; 18 | typedef unsigned short uint16_t; 19 | 20 | typedef int int32_t; 21 | typedef unsigned int uint32_t; 22 | 23 | typedef long long int64_t; 24 | typedef unsigned long long uint64_t; 25 | 26 | 27 | typedef char sint8_t; 28 | typedef short sint16_t; 29 | typedef int sint32_t; 30 | typedef long long sint64_t; 31 | 32 | 33 | // Pointers and addresses are 64 bits long. 34 | // We use pointer types to represent virtual addresses, 35 | // uintptr_t to represent the numerical values of virtual addresses, 36 | // and physaddr_t to represent physical addresses. 37 | 38 | typedef int64_t intptr_t; 39 | typedef uint64_t uintptr_t; 40 | typedef uint64_t physaddr_t; 41 | 42 | // Page numbers are 64 bits long. 43 | 44 | typedef uint64_t ppn_t; 45 | 46 | // size_t is used for memory object sizes. 47 | typedef uint64_t size_t; 48 | // ssize_t is a signed version of ssize_t, used in case there might be an 49 | // error return. 50 | typedef int64_t ssize_t; 51 | 52 | // off_t is used for file offsets and lengths. 53 | typedef int32_t off_t; 54 | 55 | // Efficient min and max operations 56 | #define MIN(_a, _b) \ 57 | ({ \ 58 | typeof(_a) __a = (_a); \ 59 | typeof(_b) __b = (_b); \ 60 | __a <= __b ? __a : __b; \ 61 | }) 62 | #define MAX(_a, _b) \ 63 | ({ \ 64 | typeof(_a) __a = (_a); \ 65 | typeof(_b) __b = (_b); \ 66 | __a >= __b ? __a : __b; \ 67 | }) 68 | 69 | // Rounding operations (efficient when n is a power of 2) 70 | // Round down to the nearest multiple of n 71 | #define ROUNDDOWN(a, n) \ 72 | ({ \ 73 | uint64_t __a = (uint64_t) (a); \ 74 | (typeof(a)) (__a - __a % (n)); \ 75 | }) 76 | // Round up to the nearest multiple of n 77 | #define ROUNDUP(a, n) \ 78 | ({ \ 79 | uint64_t __n = (uint64_t) (n); \ 80 | (typeof(a)) (ROUNDDOWN((uint64_t) (a) + __n - 1, __n)); \ 81 | }) 82 | 83 | // Return the offset of 'member' relative to the beginning of a struct type 84 | #define offsetof(type, member) ((size_t) (&((type*)0)->member)) 85 | typedef struct { 86 | int inode_num;//This field will be index in the tarfs table 87 | char filename[100]; 88 | int perm; 89 | int size; 90 | char type; 91 | int sector_loc[10]; 92 | int offset; 93 | uint64_t address; 94 | int is_fs; 95 | } file; 96 | 97 | #define SEEK_SET 1 98 | #define SEEK_CUR 2 99 | #define SEEK_END 3 100 | 101 | #endif /* DEFS_H */ 102 | -------------------------------------------------------------------------------- /include/env.h: -------------------------------------------------------------------------------- 1 | /* See COPYRIGHT for copyright information. */ 2 | 3 | #ifndef JOS_KERN_ENV_H 4 | #define JOS_KERN_ENV_H 5 | 6 | #include 7 | 8 | extern struct Env *envs; // All environments 9 | extern struct Env *curenv; // Current environment 10 | 11 | void env_init(void); 12 | void env_init_percpu(void); 13 | int env_alloc(struct Env **e, envid_t parent_id); 14 | void env_free(struct Env *e); 15 | void env_create(uint8_t *binary, size_t size, enum EnvType type); 16 | void env_destroy(struct Env *e); // Does not return if e == curenv 17 | 18 | int envid2env(envid_t envid, struct Env **env_store, bool checkperm); 19 | // The following two functions do not return 20 | void env_run(struct Env *e) __attribute__((noreturn)); 21 | void env_pop_tf(struct Trapframe *tf) __attribute__((noreturn)); 22 | 23 | // Without this extra macro, we couldn't pass macros like TEST to 24 | // ENV_CREATE because of the C pre-processor's argument prescan rule. 25 | #define ENV_PASTE3(x, y, z) x ## y ## z 26 | 27 | #define ENV_CREATE(x, type) \ 28 | do { \ 29 | extern uint8_t ENV_PASTE3(_binary_obj_, x, _start)[], \ 30 | ENV_PASTE3(_binary_obj_, x, _size)[]; \ 31 | env_create(ENV_PASTE3(_binary_obj_, x, _start), \ 32 | (int)ENV_PASTE3(_binary_obj_, x, _size), \ 33 | type); \ 34 | } while (0) 35 | 36 | #endif // !JOS_KERN_ENV_H 37 | -------------------------------------------------------------------------------- /include/error.h: -------------------------------------------------------------------------------- 1 | #ifndef ERROR_H 2 | #define ERROR_H 3 | 4 | enum { 5 | // Kernel error codes -- keep in sync with list in lib/printfmt.c. 6 | E_UNSPECIFIED = 1, // Unspecified or unknown problem 7 | E_BAD_ENV = 2, // Environment doesn't exist or otherwise 8 | // cannot be used in requested action 9 | E_INVAL = 3, // Invalid parameter 10 | E_NO_MEM = 4, // Request failed due to memory shortage 11 | E_NO_FREE_ENV = 5, // Attempt to create a new environment beyond 12 | // the maximum allowed 13 | E_FAULT = 6, // Memory fault 14 | 15 | E_IPC_NOT_RECV = 7, // Attempt to send to env that is not recving 16 | E_EOF = 8, // Unexpected end of file 17 | 18 | // File system error codes -- only seen in user-level 19 | E_NO_DISK = 9, // No free space left on disk 20 | E_MAX_OPEN = 10, // Too many files are open 21 | E_NOT_FOUND = 11, // File or block not found 22 | E_BAD_PATH = 12, // Bad path 23 | E_FILE_EXISTS = 13, // File already exists 24 | E_NOT_EXEC = 14, // File not a valid executable 25 | E_NOT_SUPP = 15, // Operation not supported 26 | 27 | MAXERROR 28 | }; 29 | 30 | #endif // !ERROR_H */ 31 | 32 | -------------------------------------------------------------------------------- /include/fs.h: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | typedef uint8_t BYTE; 4 | typedef uint16_t WORD; 5 | typedef uint32_t DWORD; 6 | typedef uint64_t QWORD; 7 | typedef enum 8 | { 9 | FIS_TYPE_REG_H2D = 0x27, // Register FIS - host to device 10 | FIS_TYPE_REG_D2H = 0x34, // Register FIS - device to host 11 | FIS_TYPE_DMA_ACT = 0x39, // DMA activate FIS - device to host 12 | FIS_TYPE_DMA_SETUP = 0x41, // DMA setup FIS - bidirectional 13 | FIS_TYPE_DATA = 0x46, // Data FIS - bidirectional 14 | FIS_TYPE_BIST = 0x58, // BIST activate FIS - bidirectional 15 | FIS_TYPE_PIO_SETUP = 0x5F, // PIO setup FIS - device to host 16 | FIS_TYPE_DEV_BITS = 0xA1, // Set device bits FIS - device to host 17 | } FIS_TYPE; 18 | 19 | typedef struct tagFIS_REG_H2D 20 | { 21 | // DWORD 0 22 | BYTE fis_type; // FIS_TYPE_REG_H2D 23 | 24 | BYTE pmport:4; // Port multiplier 25 | BYTE rsv0:3; // Reserved 26 | BYTE c:1; // 1: Command, 0: Control 27 | 28 | BYTE command; // Command register 29 | BYTE featurel; // Feature register, 7:0 30 | 31 | // DWORD 1 32 | BYTE lba0; // LBA low register, 7:0 33 | BYTE lba1; // LBA mid register, 15:8 34 | BYTE lba2; // LBA high register, 23:16 35 | BYTE device; // Device register 36 | 37 | // DWORD 2 38 | BYTE lba3; // LBA register, 31:24 39 | BYTE lba4; // LBA register, 39:32 40 | BYTE lba5; // LBA register, 47:40 41 | BYTE featureh; // Feature register, 15:8 42 | 43 | // DWORD 3 44 | BYTE countl; // Count register, 7:0 45 | BYTE counth; // Count register, 15:8 46 | BYTE icc; // Isochronous command completion 47 | BYTE control; // Control register 48 | 49 | // DWORD 4 50 | BYTE rsv1[4]; // Reserved 51 | } FIS_REG_H2D; 52 | 53 | 54 | typedef struct tagFIS_REG_D2H 55 | { 56 | // DWORD 0 57 | BYTE fis_type; // FIS_TYPE_REG_D2H 58 | 59 | BYTE pmport:4; // Port multiplier 60 | BYTE rsv0:2; // Reserved 61 | BYTE i:1; // Interrupt bit 62 | BYTE rsv1:1; // Reserved 63 | 64 | BYTE status; // Status register 65 | BYTE error; // Error register 66 | 67 | // DWORD 1 68 | BYTE lba0; // LBA low register, 7:0 69 | BYTE lba1; // LBA mid register, 15:8 70 | BYTE lba2; // LBA high register, 23:16 71 | BYTE device; // Device register 72 | 73 | // DWORD 2 74 | BYTE lba3; // LBA register, 31:24 75 | BYTE lba4; // LBA register, 39:32 76 | BYTE lba5; // LBA register, 47:40 77 | BYTE rsv2; // Reserved 78 | 79 | // DWORD 3 80 | BYTE countl; // Count register, 7:0 81 | BYTE counth; // Count register, 15:8 82 | BYTE rsv3[2]; // Reserved 83 | 84 | // DWORD 4 85 | BYTE rsv4[4]; // Reserved 86 | } FIS_REG_D2H; 87 | 88 | 89 | typedef struct tagFIS_DATA 90 | { 91 | // DWORD 0 92 | BYTE fis_type; // FIS_TYPE_DATA 93 | 94 | BYTE pmport:4; // Port multiplier 95 | BYTE rsv0:4; // Reserved 96 | 97 | BYTE rsv1[2]; // Reserved 98 | 99 | // DWORD 1 ~ N 100 | DWORD data[1]; // Payload 101 | } FIS_DATA; 102 | 103 | 104 | typedef struct tagFIS_PIO_SETUP 105 | { 106 | // DWORD 0 107 | BYTE fis_type; // FIS_TYPE_PIO_SETUP 108 | 109 | BYTE pmport:4; // Port multiplier 110 | BYTE rsv0:1; // Reserved 111 | BYTE d:1; // Data transfer direction, 1 - device to host 112 | BYTE i:1; // Interrupt bit 113 | BYTE rsv1:1; 114 | 115 | BYTE status; // Status register 116 | BYTE error; // Error register 117 | 118 | // DWORD 1 119 | BYTE lba0; // LBA low register, 7:0 120 | BYTE lba1; // LBA mid register, 15:8 121 | BYTE lba2; // LBA high register, 23:16 122 | BYTE device; // Device register 123 | 124 | // DWORD 2 125 | BYTE lba3; // LBA register, 31:24 126 | BYTE lba4; // LBA register, 39:32 127 | BYTE lba5; // LBA register, 47:40 128 | BYTE rsv2; // Reserved 129 | 130 | // DWORD 3 131 | BYTE countl; // Count register, 7:0 132 | BYTE counth; // Count register, 15:8 133 | BYTE rsv3; // Reserved 134 | BYTE e_status; // New value of status register 135 | 136 | // DWORD 4 137 | WORD tc; // Transfer count 138 | BYTE rsv4[2]; // Reserved 139 | } FIS_PIO_SETUP; 140 | 141 | 142 | 143 | typedef struct tagFIS_DMA_SETUP 144 | { 145 | // DWORD 0 146 | BYTE fis_type; // FIS_TYPE_DMA_SETUP 147 | 148 | BYTE pmport:4; // Port multiplier 149 | BYTE rsv0:1; // Reserved 150 | BYTE d:1; // Data transfer direction, 1 - device to host 151 | BYTE i:1; // Interrupt bit 152 | BYTE a:1; // Auto-activate. Specifies if DMA Activate FIS is needed 153 | 154 | BYTE rsved[2]; // Reserved 155 | 156 | //DWORD 1&2 157 | 158 | QWORD DMAbufferID; // DMA Buffer Identifier. Used to Identify DMA buffer in host memory. SATA Spec says host specific and not in Spec. Trying AHCI spec might work. 159 | 160 | //DWORD 3 161 | DWORD rsvd; //More reserved 162 | 163 | //DWORD 4 164 | DWORD DMAbufOffset; //Byte offset into buffer. First 2 bits must be 0 165 | 166 | //DWORD 5 167 | DWORD TransferCount; //Number of bytes to transfer. Bit 0 must be 0 168 | 169 | //DWORD 6 170 | DWORD resvd; //Reserved 171 | 172 | } FIS_DMA_SETUP; 173 | 174 | typedef volatile struct tagHBA_PORT 175 | { 176 | DWORD clb; // 0x00, command list base address, 1K-byte aligned 177 | DWORD clbu; // 0x04, command list base address upper 32 bits 178 | DWORD fb; // 0x08, FIS base address, 256-byte aligned 179 | DWORD fbu; // 0x0C, FIS base address upper 32 bits 180 | DWORD is; // 0x10, interrupt status 181 | DWORD ie; // 0x14, interrupt enable 182 | DWORD cmd; // 0x18, command and status 183 | DWORD rsv0; // 0x1C, Reserved 184 | DWORD tfd; // 0x20, task file data 185 | DWORD sig; // 0x24, signature 186 | DWORD ssts; // 0x28, SATA status (SCR0:SStatus) 187 | DWORD sctl; // 0x2C, SATA control (SCR2:SControl) 188 | DWORD serr; // 0x30, SATA error (SCR1:SError) 189 | DWORD sact; // 0x34, SATA active (SCR3:SActive) 190 | DWORD ci; // 0x38, command issue 191 | DWORD sntf; // 0x3C, SATA notification (SCR4:SNotification) 192 | DWORD fbs; // 0x40, FIS-based switch control 193 | DWORD rsv1[11]; // 0x44 ~ 0x6F, Reserved 194 | DWORD vendor[4]; // 0x70 ~ 0x7F, vendor specific 195 | } HBA_PORT; 196 | 197 | typedef volatile struct tagHBA_MEM 198 | { 199 | // 0x00 - 0x2B, Generic Host Control 200 | DWORD cap; // 0x00, Host capability 201 | DWORD ghc; // 0x04, Global host control 202 | DWORD is; // 0x08, Interrupt status 203 | DWORD pi; // 0x0C, Port implemented 204 | DWORD vs; // 0x10, Version 205 | DWORD ccc_ctl; // 0x14, Command completion coalescing control 206 | DWORD ccc_pts; // 0x18, Command completion coalescing ports 207 | DWORD em_loc; // 0x1C, Enclosure management location 208 | DWORD em_ctl; // 0x20, Enclosure management control 209 | DWORD cap2; // 0x24, Host capabilities extended 210 | DWORD bohc; // 0x28, BIOS/OS handoff control and status 211 | 212 | // 0x2C - 0x9F, Reserved 213 | BYTE rsv[0xA0-0x2C]; 214 | 215 | // 0xA0 - 0xFF, Vendor specific registers 216 | BYTE vendor[0x100-0xA0]; 217 | 218 | // 0x100 - 0x10FF, Port control registers 219 | HBA_PORT ports[1]; // 1 ~ 32 220 | } HBA_MEM; 221 | 222 | typedef struct tagHBA_CMD_HEADER 223 | { 224 | // DW0 225 | BYTE cfl:5; // Command FIS length in DWORDS, 2 ~ 16 226 | BYTE a:1; // ATAPI 227 | BYTE w:1; // Write, 1: H2D, 0: D2H 228 | BYTE p:1; // Prefetchable 229 | 230 | BYTE r:1; // Reset 231 | BYTE b:1; // BIST 232 | BYTE c:1; // Clear busy upon R_OK 233 | BYTE rsv0:1; // Reserved 234 | BYTE pmp:4; // Port multiplier port 235 | 236 | WORD prdtl; // Physical region descriptor table length in entries 237 | 238 | // DW1 239 | volatile 240 | DWORD prdbc; // Physical region descriptor byte count transferred 241 | 242 | // DW2, 3 243 | DWORD ctba; // Command table descriptor base address 244 | DWORD ctbau; // Command table descriptor base address upper 32 bits 245 | 246 | // DW4 - 7 247 | DWORD rsv1[4]; // Reserved 248 | } HBA_CMD_HEADER; 249 | 250 | void probe_port(HBA_MEM *abar); 251 | 252 | -------------------------------------------------------------------------------- /include/keyboard.h: -------------------------------------------------------------------------------- 1 | /* @name : keyboard.h 2 | * @author : rgolani, abmishra, skandalamsri 3 | * @last updated date : 25th September, 2013 4 | */ 5 | #ifndef X86_64_ARCH_KBC_H_ 6 | #define X86_64_ARCH_KBC_H_ 7 | 8 | #include 9 | 10 | void handle_keyboard_irq(); 11 | 12 | void kbc_initialise(); 13 | size_t kbc_bytes_in_buffer(); 14 | uint32_t kbc_readchar(); 15 | uint32_t kbc_read(void* dst, size_t size); 16 | 17 | /*Scan codes*/ 18 | enum kbsc { 19 | 20 | KBCSC_ESC = 0x01, 21 | 22 | KBCSC_1 = 0x02, 23 | KBCSC_2 = 0x03, 24 | KBCSC_3 = 0x04, 25 | KBCSC_4 = 0x05, 26 | KBCSC_5 = 0x06, 27 | KBCSC_6 = 0x07, 28 | KBCSC_7 = 0x08, 29 | KBCSC_8 = 0x09, 30 | KBCSC_9 = 0x0a, 31 | KBCSC_0 = 0x0b, 32 | 33 | KBCSC_UNDERSCORE_MINUS = 0x0c, 34 | KBCSC_EQUALS_PLUS = 0x0d, 35 | KBCSC_BACKSPACE = 0x0e, 36 | KBCSC_TAB = 0x0f, 37 | 38 | KBCSC_Q = 0x10, 39 | KBCSC_W = 0x11, 40 | KBCSC_E = 0x12, 41 | KBCSC_R = 0x13, 42 | KBCSC_T = 0x14, 43 | KBCSC_Y = 0x15, 44 | KBCSC_U = 0x16, 45 | KBCSC_I = 0x17, 46 | KBCSC_O = 0x18, 47 | KBCSC_P = 0x19, 48 | 49 | KBCSC_OPEN_CURL = 0x1a, 50 | KBCSC_CLOSE_CURL = 0x1b, 51 | 52 | KBCSC_RETURN = 0x1c, 53 | KBCSC_CTRL = 0x1d, 54 | 55 | KBCSC_A = 0x1e, 56 | KBCSC_S = 0x1f, 57 | KBCSC_D = 0x20, 58 | KBCSC_F = 0x21, 59 | KBCSC_G = 0x22, 60 | KBCSC_H = 0x23, 61 | KBCSC_J = 0x24, 62 | KBCSC_K = 0x25, 63 | KBCSC_L = 0x26, 64 | 65 | 66 | KBCSC_LEFTSHIFT = 0x2a, 67 | KBCSC_BACKSLASH = 0x2b, 68 | 69 | KBCSC_Z = 0x2c, 70 | KBCSC_X = 0x2d, 71 | KBCSC_C = 0x2e, 72 | KBCSC_V = 0x2f, 73 | KBCSC_B = 0x30, 74 | KBCSC_N = 0x31, 75 | KBCSC_M = 0x32, 76 | 77 | KBCSC_LT = 0x33, 78 | KBCSC_GT = 0x34, 79 | KBCSC_FORWARDSLASH = 0x35, 80 | KBCSC_RIGHTSHIFT = 0x36, 81 | KBCSC_PRINTSCREEN = 0x37, 82 | KBCSC_ALT = 0x38, 83 | KBCSC_SPACE = 0x39, 84 | KBCSC_CAPSLOCK = 0x3a, 85 | KBCSC_F1 = 0x3b, 86 | KBCSC_F2 = 0x3c, 87 | KBCSC_F3 = 0x3d, 88 | KBCSC_F4 = 0x3e, 89 | KBCSC_F5 = 0x3f, 90 | KBCSC_F6 = 0x40, 91 | KBCSC_F7 = 0x41, 92 | KBCSC_F8 = 0x42, 93 | KBCSC_F9 = 0x43, 94 | KBCSC_F10= 0x44, 95 | 96 | KBCSC_NUMLOCK= 0x45, 97 | KBCSC_SCROLLLOCK= 0x46, 98 | KBCSC_HOME= 0x47, 99 | KBCSC_UP= 0x48, 100 | KBCSC_PGUP= 0x49, 101 | KBCSC_NUMPAD_MINUS = 0x4a, 102 | KBCSC_NUMPAD_4 = 0x4b, 103 | KBCSC_NUMPAD_5 = 0x4c, 104 | KBCSC_NUMPAD_6 = 0x4d, 105 | KBCSC_NUMPAD_PLUS = 0x4e, 106 | KBCSC_NUMPAD_1 = 0x4f, 107 | KBCSC_NUMPAD_2 = 0x50, 108 | KBCSC_NUMPAD_3 = 0x51, 109 | KBCSC_NUMPAD_0 = 0x52, 110 | KBCSC_NUMPAD_PERIOD = 0x53, 111 | 112 | KBCSC_F11= 0x57, 113 | KBCSC_F12= 0x58, 114 | KBCSC_COLON = 0x27, 115 | KBCSC_QUOTE = 0x28, 116 | KBCSC_TILDE = 0x29, 117 | 118 | KBCSC_COMMA = 0x33, 119 | KBCSC_DOT = 0x34, 120 | KBCSC_SLASH = 0x35, 121 | KBCSC_OR = 0x2B, 122 | KBCSC_MAX = 0x60, 123 | KBCSC_LEFTCTRL = 0x1D, 124 | KBCSC_DEL = 0xE071, 125 | }; 126 | 127 | uint8_t get_tochar(uint8_t b, uint8_t shift, uint8_t ctrl); 128 | 129 | 130 | #endif /* X86_64_ARCH_KBC_H_ */ 131 | -------------------------------------------------------------------------------- /include/memlayout.h: -------------------------------------------------------------------------------- 1 | #ifndef MEMLAYOUT_H 2 | #define MEMLAYOUT_H 3 | 4 | #include 5 | #include 6 | //#include 7 | 8 | /* 9 | * This file contains definitions for memory management in our OS, 10 | * which are relevant to both the kernel and user-mode software. 11 | */ 12 | 13 | /* 14 | * Virtual memory map: Permissions 15 | * kernel/user 16 | * 17 | * 4 Gig --------> +------------------------------+ 18 | * | | RW/-- 19 | * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ 20 | * : . : 21 | * : . : 22 | * : . : 23 | * |~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~| RW/-- 24 | * | | RW/-- 25 | * | Remapped Physical Memory | RW/-- 26 | * | | RW/-- 27 | * KERNBASE, ----> +------------------------------+ 0xf0000000 --+ 28 | * KSTACKTOP | CPU0's Kernel Stack | RW/-- KSTKSIZE | 29 | * | - - - - - - - - - - - - - - -| | 30 | * | Invalid Memory (*) | --/-- KSTKGAP | 31 | * +------------------------------+ | 32 | * | CPU1's Kernel Stack | RW/-- KSTKSIZE | 33 | * | - - - - - - - - - - - - - - -| PTSIZE 34 | * | Invalid Memory (*) | --/-- KSTKGAP | 35 | * +------------------------------+ | 36 | * : . : | 37 | * : . : | 38 | * MMIOLIM ------> +------------------------------+ 0xefe00000 --+ 39 | * | Memory-mapped I/O | RW/-- PTSIZE 40 | * ULIM, MMIOBASE --> +------------------------------+ 0xefc00000 41 | * | Cur. Page Table (User R-) | R-/R- PTSIZE 42 | * UPAGES ----> +------------------------------+ 0xefa00000 43 | * | RO ENVS | R-/R- PTSIZE 44 | * UTOP,UENVS ------> +------------------------------+ 0xef800000 45 | * UXSTACKTOP -/ | User Exception Stack | RW/RW PGSIZE 46 | * +------------------------------+ 0xef7ff000 47 | * | Empty Memory (*) | --/-- PGSIZE 48 | * USTACKTOP ---> +------------------------------+ 0xef7fe000 49 | * | Normal User Stack | RW/RW PGSIZE 50 | * +------------------------------+ 0xef7fd000 51 | * | | 52 | * | | 53 | * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ 54 | * . . 55 | * . . 56 | * . . 57 | * |~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~| 58 | * | Program Data & Heap | 59 | * UTEXT --------> +------------------------------+ 0x00800000 60 | * PFTEMP -------> | Empty Memory (*) | PTSIZE 61 | * | | 62 | * UTEMP --------> +------------------------------+ 0x00400000 --+ 63 | * | Empty Memory (*) | | 64 | * | - - - - - - - - - - - - - - -| | 65 | * | User STAB Data (optional) | 2* PTSIZE 66 | * USTABDATA ----> +------------------------------+ 0x00200000 | 67 | * | Empty Memory (*) | | 68 | * 0 ------------> +------------------------------+ --+ 69 | * 70 | * (*) Note: The kernel ensures that "Invalid Memory" (ULIM) is *never* 71 | * mapped. "Empty Memory" is normally unmapped, but user programs may 72 | * map pages there if desired. JOS user programs map pages temporarily 73 | * at UTEMP. 74 | */ 75 | 76 | 77 | // All physical memory mapped at this address 78 | #define KERNBASE 0xffffffff80000000 79 | 80 | // At IOPHYSMEM (640K) there is a 384K hole for I/O. From the kernel, 81 | // IOPHYSMEM can be addressed at KERNBASE + IOPHYSMEM. The hole ends 82 | // at physical address EXTPHYSMEM. 83 | #define IOPHYSMEM 0x09fc00 84 | #define EXTPHYSMEM 0x100000 85 | 86 | 87 | #define MEMBASE 0xffffffff70000000 // 270532608 Bytes, 258 MB till (KernBase+PhysBase) 88 | 89 | #define ULIM (MEMBASE-PTSIZE) 90 | 91 | /* 92 | * User read-only mappings! Anything below here til UTOP are readonly to user. 93 | * They are global pages mapped in at env allocation time. 94 | */ 95 | 96 | // User read-only virtual page table (see 'vpt' below) 97 | 98 | #define UVPT 0x10000000000 99 | // Read-only copies of the Page structures 100 | #define UPAGES (ULIM - PTSIZE) 101 | // Read-only copies of the global env structures 102 | #define UENVS (UPAGES - PTSIZE) 103 | 104 | /* 105 | * Top of user VM. User can manipulate VA from UTOP-1 and down! 106 | */ 107 | 108 | // Top of user-accessible VM 109 | #define UTOP UENVS 110 | // Top of one-page user exception stack 111 | #define UXSTACKTOP UTOP 112 | // Next page left invalid to guard against exception stack overflow; then: 113 | // Top of normal user stack 114 | #define USTACKTOP (UTOP - 2*PGSIZE) 115 | 116 | // Where user programs generally begin 117 | #define UTEXT (4*PTSIZE) 118 | 119 | // Used for temporary page mappings. Typed 'void*' for convenience 120 | 121 | #define UTEMP ((void*) ((int)(2*PTSIZE))) 122 | // Used for temporary page mappings for the user page-fault handler 123 | // (should not conflict with other temporary page mappings) 124 | #define PFTEMP (UTEMP + PTSIZE - PGSIZE) 125 | // The location of the user-level STABS data structure 126 | #define USTABDATA (PTSIZE) 127 | 128 | // Physical address of startup code for non-boot CPUs (APs) 129 | #define MPENTRY_PADDR 0x7000 130 | 131 | 132 | typedef uint64_t pml4e_t; 133 | typedef uint64_t pdpe_t; 134 | typedef uint64_t pte_t; 135 | typedef uint64_t pde_t; 136 | 137 | /* 138 | * The page directory entry corresponding to the virtual address range 139 | * [VPT, VPT + PTSIZE) points to the page directory itself. Thus, the page 140 | * directory is treated as a page table as well as a page directory. 141 | * 142 | * One result of treating the page directory as a page table is that all PTEs 143 | * can be accessed through a "virtual page table" at virtual address VPT (to 144 | * which vpt is set in entry.S). The PTE for page number N is stored in 145 | * vpt[N]. (It's worth drawing a diagram of this!) 146 | * 147 | * A second consequence is that the contents of the current page directory 148 | * will always be available at virtual address (VPT + (VPT >> PGSHIFT)), to 149 | * which vpd is set in entry.S. 150 | */ 151 | extern volatile pte_t vpt[]; // VA of "virtual page table" 152 | extern volatile pde_t vpd[]; // VA of current page directory 153 | extern volatile pde_t vpde[]; // VA of current page directory pointer 154 | extern volatile pde_t vpml4e[]; // VA of current page map level 4 155 | 156 | //LIST_HEAD(Page_list,Page); 157 | //typedef LIST_ENTRY(Page) Page_LIST_entry_t; 158 | 159 | /* 160 | * Page descriptor structures, mapped at UPAGES. 161 | * Read/write to the kernel, read-only to user programs. 162 | * 163 | * Each struct Page stores metadata for one physical page. 164 | * Is it NOT the physical page itself, but there is a one-to-one 165 | * correspondence between physical pages and struct Page's. 166 | * You can map a Page * to the corresponding physical address 167 | * with page2pa() in kern/pmap.h. 168 | */ 169 | struct Page { 170 | // Next page on the free list. 171 | struct Page *pp_link; 172 | 173 | // pp_ref is the count of pointers (usually in page table entries) 174 | // to this page, for pages allocated using page_alloc. 175 | // Pages allocated at boot time using pmap.c's 176 | // boot_alloc do not have valid reference count fields. 177 | 178 | uint16_t pp_ref; 179 | }; 180 | 181 | #endif /* !MEMLAYOUT_H */ 182 | -------------------------------------------------------------------------------- /include/mmu.h: -------------------------------------------------------------------------------- 1 | #ifndef MMU_H 2 | #define MMU_H 3 | 4 | /* 5 | * This file contains definitions for the x86 memory management unit (MMU), 6 | * including paging- and other data structures and constants, 7 | * the %cr0, %cr4, and %eflags registers, and traps. 8 | */ 9 | 10 | /* 11 | * 12 | * Part 1. Paging data structures and constants. 13 | * 14 | */ 15 | 16 | // A linear address 'la' has a three-part structure as follows: 17 | // 18 | // +-------9--------+-------9--------+--------9-------+--------9-------+----------12---------+ 19 | // |Page Map Level 4|Page Directory | Page Directory | Page Table | Offset within Page | 20 | // | Index | Pointer Index | Index | Index | | 21 | // +----------------+----------------+----------------+--------------------------------------+ 22 | // \----PML4(la)----/\--- PDPE(la)---/\--- PDX(la) --/ \--- PTX(la) --/ \---- PGOFF(la) ----/ 23 | // \------------------------------ VPN(la) -------------------------/ 24 | // 25 | // The PML4, PDPE, PDX, PTX, PGOFF, and VPN macros decompose linear addresses as shown. 26 | // To construct a linear address la from PML4(la), PDPE(la), PDX(la), PTX(la), and PGOFF(la), 27 | // use PGADDR(PML4(la), PDPE(la), PDX(la), PTX(la), PGOFF(la)). 28 | 29 | // page number field of address 30 | #define PPN(pa) (((uintptr_t) (pa)) >> PTXSHIFT) 31 | #define VPN(la) PPN(la) // used to index into vpt[] 32 | 33 | // page directory index 34 | #define PDX(la) ((((uintptr_t) (la)) >> PDXSHIFT) & 0x1FF) 35 | #define VPD(la) (((uintptr_t) (la)) >> PDXSHIFT) // used to index into vpd[] 36 | #define VPDPE(la) (((uintptr_t) (la)) >> PDPESHIFT) 37 | #define VPML4E(la) (((uintptr_t) (la)) >> PML4SHIFT) 38 | 39 | #define PML4(la) ((((uintptr_t) (la)) >> PML4SHIFT) & 0x1FF) 40 | 41 | // page table index 42 | #define PTX(la) ((((uintptr_t) (la)) >> PTXSHIFT) & 0x1FF) 43 | #define PDPE(la) ((((uintptr_t) (la)) >> PDPESHIFT) & 0x1FF) 44 | 45 | 46 | // offset in page 47 | #define PGOFF(la) (((uintptr_t) (la)) & 0xFFF) 48 | 49 | // construct linear address from indexes and offset 50 | #define PGADDR(m,p,d, t, o) ((void*) ((m) << PML4SHIFT| (p) << PDPESHIFT | (d) << PDXSHIFT | (t) << PTXSHIFT | (o))) 51 | 52 | // Page directory and page table constants. 53 | #define NPMLENTRIES 512 // page directory entries per page directory 54 | #define NPDPENTRIES 512 // page table entries per page table 55 | #define NPDENTRIES 512 56 | #define NPTENTRIES 512 57 | 58 | #define PGSIZE 4096 // bytes mapped by a page 59 | #define PGSHIFT 12 // log2(PGSIZE) 60 | 61 | #define PTSIZE (PGSIZE*NPTENTRIES) // bytes mapped by a page directory entry 62 | #define PTSHIFT 21 // log2(PTSIZE) 63 | 64 | #define PTXSHIFT 12 // offset of PTX in a linear address 65 | #define PDXSHIFT 21 // offset of PDX in a linear address 66 | #define PDPESHIFT 30 67 | #define PML4SHIFT 39 68 | 69 | // Page table/directory entry flags. 70 | #define PTE_P 0x001 // Present 71 | #define PTE_W 0x002 // Writeable 72 | #define PTE_U 0x004 // User 73 | #define PTE_PWT 0x008 // Write-Through 74 | #define PTE_PCD 0x010 // Cache-Disable 75 | #define PTE_A 0x020 // Accessed 76 | #define PTE_D 0x040 // Dirty 77 | #define PTE_PS 0x080 // Page Size 78 | #define PTE_MBZ 0x180 // Bits must be zero 79 | 80 | // The PTE_AVAIL bits aren't used by the kernel or interpreted by the 81 | // hardware, so user processes are allowed to set them arbitrarily. 82 | #define PTE_AVAIL 0xE00 // Available for software use 83 | 84 | // Flags in PTE_SYSCALL may be used only in system calls. (Others may not.) 85 | #define PTE_SYSCALL (PTE_AVAIL | PTE_P | PTE_W | PTE_U) 86 | 87 | // Only flags in PTE_USER may be used in system calls. 88 | #define PTE_USER (PTE_AVAIL | PTE_P | PTE_W | PTE_U) 89 | 90 | // Address in page table or page directory entry 91 | #define PTE_ADDR(pte) ((physaddr_t) (pte) & ~0xFFF) 92 | 93 | // Control Register flags 94 | /* 95 | #define CR0_PE 0x00000001 // Protection Enable 96 | #define CR0_MP 0x00000002 // Monitor coProcessor 97 | #define CR0_EM 0x00000004 // Emulation 98 | #define CR0_TS 0x00000008 // Task Switched 99 | #define CR0_ET 0x00000010 // Extension Type 100 | #define CR0_NE 0x00000020 // Numeric Errror 101 | #define CR0_WP 0x00010000 // Write Protect 102 | #define CR0_AM 0x00040000 // Alignment Mask 103 | #define CR0_NW 0x20000000 // Not Writethrough 104 | #define CR0_CD 0x40000000 // Cache Disable 105 | #define CR0_PG 0x80000000 // Paging 106 | 107 | #define CR4_PCE 0x00000100 // Performance counter enable 108 | #define CR4_MCE 0x00000040 // Machine Check Enable 109 | #define CR4_PSE 0x00000010 // Page Size Extensions 110 | #define CR4_DE 0x00000008 // Debugging Extensions 111 | #define CR4_TSD 0x00000004 // Time Stamp Disable 112 | #define CR4_PVI 0x00000002 // Protected-Mode Virtual Interrupts 113 | #define CR4_VME 0x00000001 // V86 Mode Extensions 114 | 115 | //x86_64 related changes 116 | #define CR4_PAE 0x00000020 117 | #define EFER_MSR 0xC0000080 118 | #define EFER_LME 8 119 | 120 | // Eflags register 121 | #define FL_CF 0x00000001 // Carry Flag 122 | #define FL_PF 0x00000004 // Parity Flag 123 | #define FL_AF 0x00000010 // Auxiliary carry Flag 124 | #define FL_ZF 0x00000040 // Zero Flag 125 | #define FL_SF 0x00000080 // Sign Flag 126 | #define FL_TF 0x00000100 // Trap Flag 127 | #define FL_IF 0x00000200 // Interrupt Flag 128 | #define FL_DF 0x00000400 // Direction Flag 129 | #define FL_OF 0x00000800 // Overflow Flag 130 | #define FL_IOPL_MASK 0x00003000 // I/O Privilege Level bitmask 131 | #define FL_IOPL_0 0x00000000 // IOPL == 0 132 | #define FL_IOPL_1 0x00001000 // IOPL == 1 133 | #define FL_IOPL_2 0x00002000 // IOPL == 2 134 | #define FL_IOPL_3 0x00003000 // IOPL == 3 135 | #define FL_NT 0x00004000 // Nested Task 136 | #define FL_RF 0x00010000 // Resume Flag 137 | #define FL_VM 0x00020000 // Virtual 8086 mode 138 | #define FL_AC 0x00040000 // Alignment Check 139 | #define FL_VIF 0x00080000 // Virtual Interrupt Flag 140 | #define FL_VIP 0x00100000 // Virtual Interrupt Pending 141 | #define FL_ID 0x00200000 // ID flag 142 | */ 143 | // Page fault error codes 144 | #define FEC_PR 0x1 // Page fault caused by protection violation 145 | #define FEC_WR 0x2 // Page fault caused by a write 146 | #define FEC_U 0x4 // Page fault occured while in user mode 147 | 148 | #endif /* !MMU_h */ 149 | -------------------------------------------------------------------------------- /include/ports.h: -------------------------------------------------------------------------------- 1 | /* @name : ports.h 2 | * @author : rgolani, abmishra, skandalamsri 3 | * @last updated date : 25th September, 2013 4 | */ 5 | 6 | #ifndef PORTS_H 7 | #define PORTS_H 8 | 9 | #include "defs.h" 10 | 11 | inline void outb(uint16_t port, uint8_t data); 12 | void memset(void *dest, uint8_t val, uint32_t len); 13 | inline uint8_t port_inb(uint16_t port); 14 | #endif 15 | -------------------------------------------------------------------------------- /include/print.h: -------------------------------------------------------------------------------- 1 | /* @name : print.h 2 | * @author : rgolani, abmishra, skandalamsri 3 | * @last updated date : 25th September, 2013 4 | */ 5 | 6 | #ifndef WHATEVER_H_INCLUDED 7 | #define WHATEVER_H_INCLUDED 8 | void clear_screen(); 9 | int print_line(int x, int y, char *message, ...); 10 | int print(char *message, ...); 11 | void print_backspace(); 12 | void update_cursor(int row, int col); 13 | void update_cursor_current_loc(); 14 | void change_video_pointer(); 15 | void puts(char *str); 16 | void panic(char *message); 17 | #endif 18 | -------------------------------------------------------------------------------- /include/process.h: -------------------------------------------------------------------------------- 1 | /* 2 | * ===================================================================================== 3 | * 4 | * Filename: process.h 5 | * 6 | * Description: 7 | * 8 | * Version: 1.0 9 | * Created: 11/16/2013 02:33:24 AM 10 | * Revision: none 11 | * Compiler: gcc 12 | * 13 | * Author: Er. Udit Gupta (ukg), udit.gupta@stonybrook.edu 14 | * Company: Stony Brook University 15 | * 16 | * ===================================================================================== 17 | */ 18 | 19 | 20 | #ifndef _PROCESS_H 21 | #define _PROCESS_H 22 | 23 | #include 24 | #include 25 | 26 | typedef struct { 27 | uint64_t stack[1024]; // process local stack 28 | uint64_t rsp; // stack pointer 29 | uint64_t cr3; // current value of CR3 register for process switch 30 | } task; 31 | 32 | 33 | void first_cs(); 34 | void schedule(); 35 | 36 | #define yield() asm("int $0x80") 37 | 38 | task readyQ[5]; 39 | //bool sflag=true; 40 | 41 | void * get_kva(struct Page *pp); 42 | void switch_to(task* prev, task* next); 43 | 44 | task thread1; 45 | task thread2; 46 | 47 | void function1(); 48 | void function2(); 49 | 50 | 51 | #endif 52 | 53 | 54 | 55 | 56 | 57 | 58 | 59 | 60 | 61 | 62 | 63 | 64 | 65 | 66 | 67 | 68 | 69 | 70 | 71 | 72 | 73 | -------------------------------------------------------------------------------- /include/stdio.h: -------------------------------------------------------------------------------- 1 | #ifndef _STDIO_H 2 | #define _STDIO_H 3 | 4 | #include 5 | 6 | 7 | int printf(const char *format, ...); 8 | 9 | void scanf(char* str, void* buf); 10 | 11 | int printf_color(const char *fmt, ...); 12 | #endif 13 | -------------------------------------------------------------------------------- /include/stdlib.h: -------------------------------------------------------------------------------- 1 | #ifndef _STDLIB_H 2 | #define _STDLIB_H 3 | #include 4 | 5 | // Main and Exit 6 | int main(int argc, char* argv[], char* envp[]); 7 | void exit(int status); 8 | 9 | //Shutdown 10 | void shutdown(); 11 | 12 | 13 | // Process Identification 14 | typedef uint64_t pid_t; 15 | pid_t getpid (void); //The getpid function returns the process ID of the current process. 16 | pid_t getppid (void); //The getppid function returns the process ID of the parent of the current process. 17 | pid_t fork (void); // The fork function creates a new process. 18 | 19 | int execvpe (const char *filename, char *const argv[], char *const env[]); //This is similar to execv, but permits you to specify the environment for the new program explicitly as the env argument. This should be an array of strings in the same format as for the environ variable;see section Environment Access. 20 | 21 | // Memory Allocation functions 22 | void *malloc(uint64_t size); 23 | 24 | // Process Completion 25 | int waitpid(int pid,int status); 26 | 27 | //Process List 28 | void ps(); 29 | // Sleep Function 30 | uint64_t sleep(uint64_t time); 31 | 32 | //File System 33 | uint64_t opendir(char* dir); 34 | uint64_t readdir(uint64_t dir); 35 | file* open(char* file); 36 | int read(file *, int size, char * buf); 37 | int seek(file *fd, int offset, int whence); 38 | int write(file *fd, char * buf, int size); 39 | void shell_sleep(uint64_t ticks); 40 | 41 | 42 | #endif 43 | -------------------------------------------------------------------------------- /include/string.h: -------------------------------------------------------------------------------- 1 | #ifndef STRING_H 2 | #define STRING_H 3 | 4 | #include 5 | 6 | int strlen(const char *s); 7 | int strnlen(const char *s, size_t size); 8 | char * strcpy(char *dst, const char *src); 9 | char * strncpy(char *dst, const char *src, size_t size); 10 | size_t strlcpy(char *dst, const char *src, size_t size); 11 | int strcmp(const char *s1, const char *s2); 12 | int strncmp(const char *s1, const char *s2, size_t size); 13 | char * strchr(const char *s, char c); 14 | char * strfind(const char *s, char c); 15 | 16 | void * memset(void *dst, uint64_t c, size_t len); 17 | /* no memcpy - use memmove instead */ 18 | void * memmove(void *dst, const void *src, size_t len); 19 | void * memcpy(void *dst, const void *src, size_t n); 20 | int memcmp(const void *s1, const void *s2, size_t len); 21 | void * memfind(const void *s, int c, size_t len); 22 | 23 | long strtol(const char *s, char **endptr, int base); 24 | char * trimwhitespace(char *str); 25 | #endif /* STRING_H */ 26 | -------------------------------------------------------------------------------- /include/sys/env.h: -------------------------------------------------------------------------------- 1 | /* See COPYRIGHT for copyright information. */ 2 | 3 | #ifndef JOS_INC_ENV_H 4 | #define JOS_INC_ENV_H 5 | 6 | #include 7 | #include 8 | typedef uint64_t envid_t; 9 | 10 | struct PushRegs { 11 | /* registers as pushed by pusha */ 12 | uint64_t reg_edi; 13 | uint64_t reg_esi; 14 | uint64_t reg_ebp; 15 | uint64_t reg_oesp; /* Useless */ 16 | uint64_t reg_ebx; 17 | uint64_t reg_edx; 18 | uint64_t reg_ecx; 19 | uint64_t reg_eax; 20 | } __attribute__((packed)); 21 | 22 | struct Trapframe { 23 | struct PushRegs tf_regs; 24 | uint64_t tf_es; 25 | uint64_t tf_padding1; 26 | uint64_t tf_ds; 27 | uint64_t tf_padding2; 28 | uint64_t tf_trapno; 29 | /* below here defined by x86 hardware */ 30 | uint64_t tf_err; 31 | uintptr_t tf_eip; 32 | uint64_t tf_cs; 33 | uint64_t tf_padding3; 34 | uint64_t tf_eflags; 35 | /* below here only when crossing rings, such as from user to kernel */ 36 | uintptr_t tf_esp; 37 | uint64_t tf_ss; 38 | uint64_t tf_padding4; 39 | } __attribute__((packed)); 40 | 41 | 42 | // An environment ID 'envid_t' has three parts: 43 | // 44 | // +1+---------------21-----------------+--------10--------+ 45 | // |0| Uniqueifier | Environment | 46 | // | | | Index | 47 | // +------------------------------------+------------------+ 48 | // \--- ENVX(eid) --/ 49 | // 50 | // The environment index ENVX(eid) equals the environment's offset in the 51 | // 'envs[]' array. The uniqueifier distinguishes environments that were 52 | // created at different times, but share the same environment index. 53 | // 54 | // All real environments are greater than 0 (so the sign bit is zero). 55 | // envid_ts less than 0 signify errors. The envid_t == 0 is special, and 56 | // stands for the current environment. 57 | 58 | #define LOG2NENV 10 59 | #define NENV (1 << LOG2NENV) 60 | #define ENVX(envid) ((envid) & (NENV - 1)) 61 | 62 | // Values of env_status in struct Env 63 | enum { 64 | ENV_FREE = 0, 65 | ENV_RUNNABLE, 66 | ENV_RUNNING, 67 | ENV_NOT_RUNNABLE 68 | }; 69 | 70 | // Special environment types 71 | enum EnvType { 72 | ENV_TYPE_USER = 0, 73 | ENV_TYPE_IDLE, 74 | }; 75 | 76 | struct Env { 77 | struct Trapframe env_tf; // Saved registers 78 | struct Env *env_link; // Next free Env 79 | envid_t env_id; // Unique environment identifier 80 | envid_t env_parent_id; // env_id of this env's parent 81 | enum EnvType env_type; // Indicates special system environments 82 | unsigned env_status; // Status of the environment 83 | uint64_t env_runs; // Number of times environment has run 84 | 85 | // Address space 86 | pde_t *env_pgdir; // Kernel virtual address of page dir 87 | }; 88 | 89 | #endif // !JOS_INC_ENV_H 90 | -------------------------------------------------------------------------------- /include/sys/fs.h: -------------------------------------------------------------------------------- 1 | #include 2 | #define DIRECTORY 5 3 | #define FILE_TYPE 0 4 | 5 | typedef struct { 6 | char fs_type[10]; 7 | int size; 8 | int magic_no; 9 | int inode_start_sector; 10 | int data_start_sector; 11 | int free_inode_block[4]; 12 | int free_data_block[1272]; 13 | } superblock; 14 | 15 | typedef struct { 16 | int inode_num; 17 | char filename[100]; 18 | int perm; 19 | int size; 20 | char type; 21 | int sector_loc[10]; 22 | int written; 23 | } inode; 24 | 25 | typedef struct { 26 | char filename[100]; 27 | int inode_num; 28 | } dentry; 29 | 30 | typedef struct { 31 | char inode_num; 32 | char name[100]; 33 | int size; 34 | int typeflag; 35 | int par_ind; 36 | } satafs_entry; 37 | 38 | extern satafs_entry sata_fs[100]; 39 | #define SUPERBLOCK_SIZE 20 40 | #define SUPERBLOCK_S 1 41 | #define SUPERBLOCK_E 20 42 | #define INODE_S 23 43 | #define INODE_E 151 44 | #define RESERVE_I1_S 152 45 | #define RESERVE_I1_E 160 46 | #define SUPERBLOCK_COPY_S 161 47 | #define SUPERBLOCK_COPY_E 180 48 | #define RESERVE_I2_S 181 49 | #define RESERVE_I2_E 190 50 | #define DATA_S 191 51 | #define DATA_E 40704 52 | #define SECTOR_SIZE 512 53 | #define INODES_PER_SECTOR 3 54 | 55 | void initialize_superblock(); 56 | void inialize_sata_table(); 57 | file* file_open(char *file_name); 58 | int file_read(void *ptr, size_t nitems, file *fd); 59 | int file_seek(file *fd, int offset, int whence); 60 | int dir_read(char * name); 61 | int file_write(file *fd, char * buf, int size); 62 | int dir_write(char * name); 63 | int dir_open(char * name); 64 | int make_dir(char * name); 65 | int file_close(file *fd); 66 | void hdd_ls(); 67 | extern uint64_t superblock_start_addr_p ; 68 | extern uint64_t superblock_start_addr; 69 | extern superblock* superblock_fs ; 70 | extern uint64_t inode_mgmt_buffer_p; 71 | extern uint64_t inode_mgmt_buffer; 72 | extern uint64_t data_mgmt_buffer_p; 73 | extern uint64_t data_mgmt_buffer; 74 | -------------------------------------------------------------------------------- /include/sys/gdt.h: -------------------------------------------------------------------------------- 1 | #ifndef _GDT_H 2 | #define _GDT_H 3 | 4 | #include 5 | 6 | struct tss_t { 7 | uint32_t reserved; 8 | uint64_t rsp0; 9 | uint32_t unused[11]; 10 | }__attribute__((packed)) tss; 11 | 12 | /* adapted from Chris Stones, shovelos */ 13 | 14 | #define GDT_CS (0x00180000000000) /*** code segment descriptor ***/ 15 | #define GDT_DS (0x00100000000000) /*** data segment descriptor ***/ 16 | 17 | #define C (0x00040000000000) /*** conforming ***/ 18 | #define DPL0 (0x00000000000000) /*** descriptor privilege level 0 ***/ 19 | #define DPL1 (0x00200000000000) /*** descriptor privilege level 1 ***/ 20 | #define DPL2 (0x00400000000000) /*** descriptor privilege level 2 ***/ 21 | #define DPL3 (0x00600000000000) /*** descriptor privilege level 3 ***/ 22 | #define P (0x00800000000000) /*** present ***/ 23 | #define L (0x20000000000000) /*** long mode ***/ 24 | #define D (0x40000000000000) /*** default op size ***/ 25 | #define W (0x00020000000000) /*** writable data segment ***/ 26 | 27 | extern uint64_t gdt[]; 28 | 29 | void reload_gdt(); 30 | void setup_tss(); 31 | 32 | #endif 33 | -------------------------------------------------------------------------------- /include/sys/idt.h: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | void reload_idt(); 4 | void load_irq(); 5 | struct idt_entry_struct 6 | { 7 | uint16_t offset_low; 8 | uint16_t selector; 9 | unsigned ist : 3; 10 | unsigned reserved0 : 5; 11 | unsigned type : 4; 12 | unsigned zero : 1; 13 | unsigned dpl : 2; 14 | unsigned p : 1; 15 | uint16_t offset_mid; 16 | uint32_t offset_high; 17 | uint32_t reserved1; 18 | } __attribute__((packed)); 19 | 20 | 21 | 22 | typedef struct idt_entry_struct idt_entry_t; 23 | 24 | struct idt_ptr_struct 25 | { 26 | uint16_t limit; 27 | uint64_t base; 28 | } __attribute__ ((packed)); 29 | 30 | typedef struct idt_ptr_struct idt_ptr_t; 31 | extern void _x86_64_asm_igdt(struct idt_ptr_struct* idtr); 32 | 33 | -------------------------------------------------------------------------------- /include/sys/isr.h: -------------------------------------------------------------------------------- 1 | #ifndef __KERNEL_ARCH_X86_64_ISR_H 2 | #define __KERNEL_ARCH_X86_64_ISR_H 3 | #define IRQ0 32 4 | #define KBC_VECTOR 64 5 | 6 | struct isr_error_stack_frame 7 | { 8 | uint64_t error; 9 | 10 | uint64_t r15; 11 | uint64_t r14; 12 | uint64_t r13; 13 | uint64_t r12; 14 | uint64_t r11; 15 | uint64_t r10; 16 | uint64_t r9; 17 | uint64_t r8; 18 | uint64_t rdi; 19 | uint64_t rsi; 20 | uint64_t rdx; 21 | uint64_t rcx; 22 | uint64_t rbx; 23 | uint64_t rax; 24 | 25 | uint64_t rip; 26 | uint64_t cs; 27 | uint64_t rflags; 28 | uint64_t rsp; 29 | uint64_t ss; 30 | }; 31 | 32 | struct isr_stack_frame 33 | { 34 | uint64_t r15; 35 | uint64_t r14; 36 | uint64_t r13; 37 | uint64_t r12; 38 | uint64_t r11; 39 | uint64_t r10; 40 | uint64_t r9; 41 | uint64_t r8; 42 | uint64_t rdi; 43 | uint64_t rsi; 44 | uint64_t rdx; 45 | uint64_t rcx; 46 | uint64_t rbx; 47 | uint64_t rax; 48 | 49 | uint64_t rip; 50 | uint64_t cs; 51 | uint64_t rflags; 52 | uint64_t rsp; 53 | uint64_t ss; 54 | }; 55 | 56 | struct isr_pf_stack_frame 57 | { 58 | union { 59 | uint64_t reserved; 60 | struct { 61 | unsigned p : 1; 62 | unsigned wr : 1; 63 | unsigned us : 1; 64 | unsigned rsvd : 1; 65 | unsigned id : 1; 66 | }error; 67 | }error; 68 | 69 | uint64_t r15; 70 | uint64_t r14; 71 | uint64_t r13; 72 | uint64_t r12; 73 | uint64_t r11; 74 | uint64_t r10; 75 | uint64_t r9; 76 | uint64_t r8; 77 | uint64_t rdi; 78 | uint64_t rsi; 79 | uint64_t rdx; 80 | uint64_t rcx; 81 | uint64_t rbx; 82 | uint64_t rax; 83 | 84 | uint64_t rip; 85 | uint64_t cs; 86 | uint64_t rflags; 87 | uint64_t rsp; 88 | uint64_t ss; 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 | #endif /*** __KERNEL_ARCH_X86_64_ISR_H ***/ 135 | 136 | 137 | -------------------------------------------------------------------------------- /include/sys/kbc.h: -------------------------------------------------------------------------------- 1 | /* kbc.h 2 | * 3 | * Created on: 9 Aug 2011 4 | * Author: Chris Stones 5 | */ 6 | 7 | #ifndef X86_64_ARCH_KBC_H_ 8 | #define X86_64_ARCH_KBC_H_ 9 | 10 | #include 11 | 12 | void kbc_irq(); 13 | 14 | void kbc_initialise(); 15 | size_t kbc_bytes_in_buffer(); 16 | sint32_t kbc_readchar(); 17 | sint32_t kbc_read(void* dst, size_t size); 18 | void scanf(char *); 19 | /*Scan codes*/ 20 | extern char keyboard_buf[100]; 21 | extern volatile int is_taking_input; 22 | 23 | uint8_t get_tochar(uint8_t b, uint8_t shift , uint8_t ctrl); 24 | #endif /* X86_64_ARCH_KBC_H_ */ 25 | -------------------------------------------------------------------------------- /include/sys/kbc_scancodes.h: -------------------------------------------------------------------------------- 1 | #ifndef ARCH_KBC_SCANCODES_H 2 | #define ARCH_KBC_SCANCODES_H 3 | 4 | #include 5 | 6 | enum kbsc { 7 | 8 | KBCSC_ESC = 0x01, 9 | 10 | KBCSC_1 = 0x02, 11 | KBCSC_2 = 0x03, 12 | KBCSC_3 = 0x04, 13 | KBCSC_4 = 0x05, 14 | KBCSC_5 = 0x06, 15 | KBCSC_6 = 0x07, 16 | KBCSC_7 = 0x08, 17 | KBCSC_8 = 0x09, 18 | KBCSC_9 = 0x0a, 19 | KBCSC_0 = 0x0b, 20 | 21 | KBCSC_UNDERSCORE_MINUS = 0x0c, 22 | KBCSC_EQUALS_PLUS = 0x0d, 23 | KBCSC_BACKSPACE = 0x0e, 24 | KBCSC_TAB = 0x0f, 25 | 26 | KBCSC_Q = 0x10, 27 | KBCSC_W = 0x11, 28 | KBCSC_E = 0x12, 29 | KBCSC_R = 0x13, 30 | KBCSC_T = 0x14, 31 | KBCSC_Y = 0x15, 32 | KBCSC_U = 0x16, 33 | KBCSC_I = 0x17, 34 | KBCSC_O = 0x18, 35 | KBCSC_P = 0x19, 36 | 37 | KBCSC_OPEN_CURL = 0x1a, 38 | KBCSC_CLOSE_CURL = 0x1b, 39 | 40 | KBCSC_RETURN = 0x1c, 41 | KBCSC_CTRL = 0x1d, 42 | 43 | KBCSC_A = 0x1e, 44 | KBCSC_S = 0x1f, 45 | KBCSC_D = 0x20, 46 | KBCSC_F = 0x21, 47 | KBCSC_G = 0x22, 48 | KBCSC_H = 0x23, 49 | KBCSC_J = 0x24, 50 | KBCSC_K = 0x25, 51 | KBCSC_L = 0x26, 52 | 53 | KBCSC_COLON = 0x27, 54 | KBCSC_AT = 0x28, 55 | 56 | KBCSC_TILDA = 0x29, 57 | 58 | KBCSC_LEFTSHIFT = 0x2a, 59 | KBCSC_BACKSLASH = 0x2b, 60 | 61 | KBCSC_Z = 0x2c, 62 | KBCSC_X = 0x2d, 63 | KBCSC_C = 0x2e, 64 | KBCSC_V = 0x2f, 65 | KBCSC_B = 0x30, 66 | KBCSC_N = 0x31, 67 | KBCSC_M = 0x32, 68 | 69 | KBCSC_LT = 0x33, 70 | KBCSC_GT = 0x34, 71 | KBCSC_FORWARDSLASH = 0x35, 72 | KBCSC_RIGHTSHIFT = 0x36, 73 | KBCSC_PRINTSCREEN = 0x37, 74 | KBCSC_ALT = 0x38, 75 | KBCSC_SPACE = 0x39, 76 | KBCSC_CAPSLOCK = 0x3a, 77 | KBCSC_F1 = 0x3b, 78 | KBCSC_F2 = 0x3c, 79 | KBCSC_F3 = 0x3d, 80 | KBCSC_F4 = 0x3e, 81 | KBCSC_F5 = 0x3f, 82 | KBCSC_F6 = 0x40, 83 | KBCSC_F7 = 0x41, 84 | KBCSC_F8 = 0x42, 85 | KBCSC_F9 = 0x43, 86 | KBCSC_F10= 0x44, 87 | 88 | KBCSC_NUMLOCK= 0x45, 89 | KBCSC_SCROLLLOCK= 0x46, 90 | KBCSC_HOME= 0x47, 91 | KBCSC_UP= 0x48, 92 | KBCSC_PGUP= 0x49, 93 | KBCSC_NUMPAD_MINUS = 0x4a, 94 | KBCSC_NUMPAD_4 = 0x4b, 95 | KBCSC_NUMPAD_5 = 0x4c, 96 | KBCSC_NUMPAD_6 = 0x4d, 97 | KBCSC_NUMPAD_PLUS = 0x4e, 98 | KBCSC_NUMPAD_1 = 0x4f, 99 | KBCSC_NUMPAD_2 = 0x50, 100 | KBCSC_NUMPAD_3 = 0x51, 101 | KBCSC_NUMPAD_0 = 0x52, 102 | KBCSC_NUMPAD_PERIOD = 0x53, 103 | 104 | KBCSC_F11= 0x57, 105 | KBCSC_F12= 0x58, 106 | KBCSC_COMMA = 0x33, 107 | KBCSC_SLASH = 0x35, 108 | KBCSC_MAX = 0x60, 109 | KBCSC_OR = 0x2B 110 | }; 111 | 112 | uint8_t kbcsc_tochar(uint8_t b, uint8_t shift); 113 | 114 | #endif /*** ARCH_KBC_SCANCODES_H ***/ 115 | 116 | 117 | -------------------------------------------------------------------------------- /include/sys/memlayout.h: -------------------------------------------------------------------------------- 1 | #ifndef MEMLAYOUT_H 2 | #define MEMLAYOUT_H 3 | 4 | #include 5 | #include 6 | // All physical memory mapped at this address 7 | #define KERNBASE 0xffffffff80000000 8 | 9 | // at physical address EXTPHYSMEM. 10 | #define IOPHYSMEM 0x09fc00 11 | #define EXTPHYSMEM 0x100000 12 | 13 | 14 | #define ULIM (KERNBASE-PGSIZE) 15 | #define USTACKTOP ULIM 16 | #define USTACK 0xFFFFF0F080700000 17 | 18 | 19 | // User read-only virtual page table (see 'vpt' below) 20 | 21 | #define UVPT 0x10000000000 22 | // Read-only copies of the Page structures 23 | #define UPAGES (ULIM - PTSIZE) 24 | // Read-only copies of the global env structures 25 | #define UENVS (UPAGES - PTSIZE) 26 | 27 | /* 28 | * Top of user VM. User can manipulate VA from UTOP-1 and down! 29 | */ 30 | 31 | // Top of user-accessible VM 32 | #define UTOP UENVS 33 | 34 | // Where user programs generally begin 35 | #define UTEXT (4*PTSIZE) 36 | 37 | // Used for temporary page mappings. Typed 'void*' for convenience 38 | 39 | #define UTEMP ((void*) ((int)(2*PTSIZE))) 40 | // Used for temporary page mappings for the user page-fault handler 41 | // (should not conflict with other temporary page mappings) 42 | #define PFTEMP (UTEMP + PTSIZE - PGSIZE) 43 | // The location of the user-level STABS data structure 44 | #define USTABDATA (PTSIZE) 45 | 46 | // Physical address of startup code for non-boot CPUs (APs) 47 | #define MPENTRY_PADDR 0x7000 48 | 49 | 50 | typedef uint64_t pml4e_t; 51 | typedef uint64_t pdpe_t; 52 | typedef uint64_t pte_t; 53 | typedef uint64_t pde_t; 54 | extern volatile pte_t vpt[]; // VA of "virtual page table" 55 | extern volatile pde_t vpd[]; // VA of current page directory 56 | extern volatile pde_t vpde[]; // VA of current page directory pointer 57 | extern volatile pde_t vpml4e[]; // VA of current page map level 4 58 | struct Page { 59 | // Next page on the free list. 60 | struct Page *pp_link; 61 | 62 | // pp_ref is the count of pointers (usually in page table entries) 63 | // to this page, for pages allocated using page_alloc. 64 | // Pages allocated at boot time using pmap.c's 65 | // boot_alloc do not have valid reference count fields. 66 | 67 | uint16_t pp_ref; 68 | }; 69 | 70 | #endif /* !MEMLAYOUT_H */ 71 | -------------------------------------------------------------------------------- /include/sys/mmu.h: -------------------------------------------------------------------------------- 1 | #ifndef MMU_H 2 | #define MMU_H 3 | // page number field of address 4 | #define PPN(pa) (((uintptr_t) (pa)) >> PTXSHIFT) 5 | #define VPN(la) PPN(la) // used to index into vpt[] 6 | 7 | // page directory index 8 | #define PDX(la) ((((uintptr_t) (la)) >> PDXSHIFT) & 0x1FF) 9 | #define VPD(la) (((uintptr_t) (la)) >> PDXSHIFT) // used to index into vpd[] 10 | #define VPDPE(la) (((uintptr_t) (la)) >> PDPESHIFT) 11 | #define VPML4E(la) (((uintptr_t) (la)) >> PML4SHIFT) 12 | 13 | #define PML4(la) ((((uintptr_t) (la)) >> PML4SHIFT) & 0x1FF) 14 | 15 | // page table index 16 | #define PTX(la) ((((uintptr_t) (la)) >> PTXSHIFT) & 0x1FF) 17 | #define PDPE(la) ((((uintptr_t) (la)) >> PDPESHIFT) & 0x1FF) 18 | 19 | 20 | // offset in page 21 | #define PGOFF(la) (((uintptr_t) (la)) & 0xFFF) 22 | 23 | // construct linear address from indexes and offset 24 | #define PGADDR(m,p,d, t, o) ((void*) ((m) << PML4SHIFT| (p) << PDPESHIFT | (d) << PDXSHIFT | (t) << PTXSHIFT | (o))) 25 | 26 | // Page directory and page table constants. 27 | #define NPMLENTRIES 512 // page directory entries per page directory 28 | #define NPDPENTRIES 512 // page table entries per page table 29 | #define NPDENTRIES 512 30 | #define NPTENTRIES 512 31 | 32 | #define PGSIZE 4096 // bytes mapped by a page 33 | #define PGSHIFT 12 // log2(PGSIZE) 34 | 35 | #define PTSIZE (PGSIZE*NPTENTRIES) // bytes mapped by a page directory entry 36 | #define PTSHIFT 21 // log2(PTSIZE) 37 | 38 | #define PTXSHIFT 12 // offset of PTX in a linear address 39 | #define PDXSHIFT 21 // offset of PDX in a linear address 40 | #define PDPESHIFT 30 41 | #define PML4SHIFT 39 42 | 43 | // Page table/directory entry flags. 44 | #define PTE_P 0x001 // Present 45 | #define PTE_W 0x002 // Writeable 46 | #define PTE_U 0x004 // User 47 | #define PTE_PWT 0x008 // Write-Through 48 | #define PTE_PCD 0x010 // Cache-Disable 49 | #define PTE_A 0x020 // Accessed 50 | #define PTE_D 0x040 // Dirty 51 | #define PTE_PS 0x080 // Page Size 52 | #define PTE_MBZ 0x180 // Bits must be zero 53 | 54 | // The PTE_AVAIL bits aren't used by the kernel or interpreted by the 55 | // hardware, so user processes are allowed to set them arbitrarily. 56 | #define PTE_AVAIL 0xE00 // Available for software use 57 | 58 | // Flags in PTE_SYSCALL may be used only in system calls. (Others may not.) 59 | #define PTE_SYSCALL (PTE_AVAIL | PTE_P | PTE_W | PTE_U) 60 | 61 | // Only flags in PTE_USER may be used in system calls. 62 | #define PTE_USER (PTE_AVAIL | PTE_P | PTE_W | PTE_U) 63 | 64 | // Address in page table or page directory entry 65 | #define PTE_ADDR(pte) ((physaddr_t) (pte) & ~0xFFF) 66 | 67 | // Page fault error codes 68 | #define FEC_PR 0x1 // Page fault caused by protection violation 69 | #define FEC_WR 0x2 // Page fault caused by a write 70 | #define FEC_U 0x4 // Page fault occured while in user mode 71 | 72 | #endif /* !MMU_h */ 73 | -------------------------------------------------------------------------------- /include/sys/pmap.h: -------------------------------------------------------------------------------- 1 | 2 | #ifndef PMAP_H 3 | #define PMAP_H 4 | 5 | 6 | #include 7 | #include 8 | #include 9 | #include 10 | 11 | struct Env; 12 | 13 | extern char bootstacktop[], bootstack[]; 14 | 15 | extern struct Page *pages; 16 | extern size_t npages; 17 | 18 | extern pml4e_t *boot_pml4e; 19 | extern uint64_t bar5; 20 | #define PAGE_TABLE_ALIGNLENT 0x1000 21 | 22 | /* mask out bits under page size */ 23 | #define ALIGN_DOWN(x) (x & ~(PAGE_TABLE_ALIGNLENT-1)) 24 | 25 | 26 | 27 | 28 | #define PADDR(kva) \ 29 | ({ \ 30 | physaddr_t __m_kva = (physaddr_t) (kva); \ 31 | if (__m_kva < KERNBASE) \ 32 | print("PADDR called with invalid kva %08lx", __m_kva);\ 33 | __m_kva - KERNBASE; \ 34 | }) 35 | 36 | /* This macro takes a physical address and returns the corresponding kernel 37 | * virtual address. It panics if you pass an invalid physical address. */ 38 | #define KADDR(pa) \ 39 | ({ \ 40 | physaddr_t __m_pa = (pa); \ 41 | uint32_t __m_ppn = PPN(__m_pa);\ 42 | if (__m_ppn >= npages) \ 43 | print("KADDR called with invalid pa %08lx", __m_pa);\ 44 | (void*) ((uint64_t)(__m_pa + KERNBASE)); \ 45 | }) 46 | 47 | 48 | enum { 49 | // For page_alloc, zero the returned physical page. 50 | ALLOC_ZERO = 1<<0, 51 | }; 52 | 53 | 54 | void mm_init(uint32_t* , void* , void*); 55 | void *boot_alloc(uint64_t n); 56 | void boot_map_segment(pml4e_t *pml4e, uintptr_t la, size_t size, physaddr_t pa, int perm); 57 | void page_init(void); 58 | struct Page * page_alloc(int alloc_flags); 59 | void page_free(struct Page *pp); 60 | int page_insert(pml4e_t *pml4e, struct Page *pp, void *va, int perm); 61 | void page_remove(pml4e_t *pml4e, void *va); 62 | struct Page *page_lookup(pml4e_t *pml4e, void *va, pte_t **pte_store); 63 | void page_decref(struct Page *pp); 64 | 65 | void tlb_invalidate(pml4e_t *pml4e, void *va); 66 | 67 | void * mmio_map_region(physaddr_t pa, size_t size); 68 | 69 | int user_mem_check(struct Env *env, const void *va, size_t len, int perm); 70 | void user_mem_assert(struct Env *env, const void *va, size_t len, int perm); 71 | 72 | 73 | 74 | 75 | uint64_t * vmmngr_bump_alloc_process (uint64_t **bumpPtr,uint64_t size); 76 | uint64_t * vmmngr_bump_alloc (uint64_t size); 77 | uint64_t *kmalloc(uint64_t size); 78 | uint64_t *kmalloc_user(pml4e_t *pml4e,uint64_t size); 79 | 80 | 81 | 82 | 83 | static inline ppn_t 84 | page2ppn(struct Page *pp) 85 | { 86 | return pp - pages; 87 | } 88 | 89 | static inline physaddr_t 90 | page2pa(struct Page *pp) 91 | { 92 | return page2ppn(pp) << PGSHIFT; 93 | 94 | } 95 | 96 | static inline struct Page* 97 | pa2page(physaddr_t pa) 98 | { 99 | if (PPN(pa) >= npages) 100 | print("pa2page called with invalid pa"); 101 | print("\npage 2 pa %x", &pages[PPN(pa)]); 102 | return &pages[PPN(pa)]; 103 | } 104 | 105 | static inline void* 106 | page2kva(struct Page *pp) 107 | { 108 | return KADDR(page2pa(pp)); 109 | } 110 | 111 | pte_t *pgdir_walk(pde_t *pgdir, const void *va, int create); 112 | 113 | pte_t *pml4e_walk(pml4e_t *pml4e, const void *va, int create); 114 | 115 | pde_t *pdpe_walk(pdpe_t *pdpe,const void *va,int create); 116 | 117 | uint64_t kmalloc_ahci(uint64_t size); 118 | #endif /* PMAP_H */ 119 | -------------------------------------------------------------------------------- /include/sys/ports.h: -------------------------------------------------------------------------------- 1 | #ifndef PORTS_H 2 | #define PORTS_H 3 | 4 | #include "defs.h" 5 | #include "string.h" 6 | 7 | void outb(uint16_t port, uint8_t data); 8 | inline uint8_t port_inb(uint16_t port); 9 | #endif 10 | -------------------------------------------------------------------------------- /include/sys/print.h: -------------------------------------------------------------------------------- 1 | /* @name : print.h 2 | * @author : rgolani, abmishra, skandalamsri 3 | * @last updated date : 25th September, 2013 4 | */ 5 | 6 | #ifndef WHATEVER_H_INCLUDED 7 | #define WHATEVER_H_INCLUDED 8 | void clear_screen(); 9 | int print_line(int x, int y, char *message, ...); 10 | int print(char *message, ...); 11 | void print_backspace(); 12 | void update_cursor(int row, int col); 13 | void update_cursor_current_loc(); 14 | void change_video_pointer(); 15 | void puts(char *str); 16 | void panic(char *message); 17 | int putc(char ch); 18 | int putc_color(char ch); 19 | 20 | 21 | #endif 22 | -------------------------------------------------------------------------------- /include/sys/process.h: -------------------------------------------------------------------------------- 1 | /* 2 | * ===================================================================================== 3 | * 4 | * Filename: process.h 5 | * 6 | * Description: 7 | * 8 | * Version: 1.0 9 | * Created: 11/16/2013 02:33:24 AM 10 | * Revision: none 11 | * Compiler: gcc 12 | * 13 | * Author: Er. Udit Gupta (ukg), udit.gupta@stonybrook.edu 14 | * Company: Stony Brook University 15 | * 16 | * ===================================================================================== 17 | */ 18 | 19 | 20 | #ifndef _PROCESS_H 21 | #define _PROCESS_H 22 | 23 | #include 24 | #include 25 | 26 | struct vm_area_struct{ 27 | struct mm_struct * vm_mm; /* associated mm_struct */ 28 | uint64_t vm_start; /* VMA start, inclusive */ 29 | uint64_t vm_end; /* VMA end, exclusive */ 30 | uint64_t vm_mmsz; /* VMA size */ 31 | unsigned long vm_flags; /* flags */ 32 | uint32_t grows_down; 33 | uint64_t vm_file; /* mapped file, if any */ 34 | struct vm_area_struct *vm_next;/* list of VMA's */ 35 | uint64_t vm_offset; /* file offset */ 36 | }; 37 | 38 | 39 | struct mm_struct { 40 | int count; 41 | uint64_t * pt; // page table pointer 42 | unsigned long context; 43 | unsigned long start_code, end_code, start_data, end_data; 44 | unsigned long start_brk, brk, start_stack, start_mmap; 45 | unsigned long arg_start, arg_end, env_start, env_end; 46 | unsigned long rss, total_vm, locked_vm; 47 | unsigned long def_flags; 48 | struct vm_area_struct * mmap; 49 | struct vm_area_struct * mmap_avl; 50 | }; 51 | 52 | 53 | struct vm_area_struct *malloc_vma(struct mm_struct *mm); 54 | //struct task_struct * malloc_pcb(struct task_struct *,void *fun_ptr); 55 | struct task_struct * malloc_pcb(void *fun_ptr); 56 | 57 | /****************** Process **********************/ 58 | //PCB structure 59 | struct task_struct{ 60 | uint64_t state; //0 runnable ,-1 unrunnable, >0 stopped 61 | uint64_t counter; //how long its run; 62 | uint64_t pid; //my id; 63 | uint64_t ppid; //my parent pid 64 | uint64_t priority; // when should it run 65 | uint64_t *stack; // its own stack 66 | uint64_t *rsp; // process stack pointer 67 | uint64_t pml4e; // will directly go to cr3 register 68 | uint64_t cr3; 69 | uint64_t *bmpPtr; // user stack of the process 70 | struct mm_struct *mm; // a pointer to mm_struct 71 | uint64_t rip; // instruction pointer 72 | uint64_t status; // exit status 73 | uint64_t zombie; // mark as zombie process 74 | uint64_t kstack[512]; 75 | struct vm_area_struct *heap_vma; // vma for heap 76 | int sleep_time; 77 | uint64_t entry; 78 | int is_waiting; 79 | char pname[100]; //process name 80 | }; 81 | 82 | void run_process(struct task_struct **pcb); 83 | uint64_t ready_queue[100]; // can accomodate 100 process now 84 | uint64_t zombie_queue[100] ; // queue of all process which contains exit status of zombie process 85 | uint64_t sleep_queue[100]; // To keep track the time quantum 86 | 87 | 88 | void sleep_process(); 89 | 90 | void exit_process(int i); // exit process 91 | int get_pid(); 92 | int get_ppid(); 93 | struct task_struct* get_current_process(); 94 | void* malloc(uint64_t size); 95 | int fork(); 96 | void fork_process(); 97 | int sleep(int j); 98 | 99 | 100 | uint64_t sleeping_process_count; // keeps track the number of sleeping process 101 | uint64_t process_count; // keep tracks the number of current process in ready_queue(run_queue) 102 | 103 | void schedule(); 104 | 105 | #define yield() asm("int $0x80") 106 | 107 | void * get_kva(struct Page *pp); 108 | 109 | void function1(); 110 | void function2(); 111 | 112 | void load_icode(struct task_struct *t, char *filename, size_t size); 113 | 114 | static __inline void lcr3(uint64_t val) { 115 | __asm __volatile("movq %0,%%cr3" : : "r" (val)); 116 | } 117 | 118 | 119 | uint64_t sleeping_process_count; // keeps track the number of sleeping process 120 | uint64_t process_count; // keep tracks the number of current process in ready_queue(run_queue) 121 | struct vm_area_struct *malloc_vma(struct mm_struct *mm); 122 | 123 | void region_alloc(struct task_struct *t, void *va, size_t len); 124 | 125 | int get_pid(); 126 | int get_ppid(); 127 | int fork(); 128 | 129 | void exit_process(int status); 130 | 131 | int sleep(int t); 132 | 133 | void* malloc(uint64_t size); 134 | 135 | uint64_t execvpe(char *arg1,char *arg2[], char* arg3[]); 136 | int waitpid(int pid, int status) ; 137 | 138 | void dp(); 139 | void kshutdown(); 140 | #endif 141 | -------------------------------------------------------------------------------- /include/sys/tarfs.h: -------------------------------------------------------------------------------- 1 | #ifndef _TARFS_H 2 | #define _TARFS_H 3 | #include 4 | extern char _binary_tarfs_start; 5 | extern char _binary_tarfs_end; 6 | 7 | #define ELF_MAGIC 0x464C457F 8 | #define ELF_PROG_LOAD 1 9 | 10 | struct posix_header_ustar { 11 | char name[100]; 12 | char mode[8]; 13 | char uid[8]; 14 | char gid[8]; 15 | char size[12]; 16 | char mtime[12]; 17 | char checksum[8]; 18 | char typeflag[1]; 19 | char linkname[100]; 20 | char magic[6]; 21 | char version[2]; 22 | char uname[32]; 23 | char gname[32]; 24 | char devmajor[8]; 25 | char devminor[8]; 26 | char prefix[155]; 27 | char pad[12]; 28 | }; 29 | 30 | #define EI_NIDENT 16 31 | 32 | typedef struct { 33 | unsigned char e_ident[EI_NIDENT]; 34 | uint16_t e_type; 35 | uint16_t e_machine; 36 | uint32_t e_version; 37 | uint64_t e_entry; 38 | uint64_t e_phoff; 39 | uint64_t e_shoff; 40 | uint32_t e_flags; 41 | uint16_t e_ehsize; 42 | uint16_t e_phentsize; 43 | uint16_t e_phnum; 44 | uint16_t e_shentsize; 45 | uint16_t e_shnum; 46 | uint16_t e_shstrndx; 47 | } Elf_hdr; 48 | 49 | typedef struct { 50 | uint32_t p_type; 51 | uint32_t p_flags; 52 | uint64_t p_offset; 53 | uint64_t p_vaddr; 54 | uint64_t p_paddr; 55 | uint64_t p_filesz; 56 | uint64_t p_memsz; 57 | uint64_t p_align; 58 | } Elf64_Phdr; 59 | 60 | typedef struct { 61 | uint32_t sh_name; 62 | uint32_t sh_type; 63 | uint64_t sh_flags; 64 | uint64_t sh_addr; 65 | uint64_t sh_offset; 66 | uint64_t sh_size; 67 | uint32_t sh_link; 68 | uint32_t sh_info; 69 | uint64_t sh_addralign; 70 | uint64_t sh_entsize; 71 | } Elf64_Shdr; 72 | 73 | typedef struct { 74 | unsigned char e_ident[16]; 75 | uint32_t e_type; 76 | uint32_t e_machine; 77 | uint64_t e_version; 78 | uint64_t e_entry; 79 | uint64_t e_phoff; 80 | uint64_t e_shoff; 81 | uint64_t e_flags; 82 | uint32_t e_ehsize; 83 | uint32_t e_phentsize; 84 | uint32_t e_phnum; 85 | uint32_t e_shentsize; 86 | uint32_t e_shnum; 87 | uint32_t e_shtrndx; 88 | } Elf64_Ehdr; 89 | 90 | uint64_t is_file_exists(char* filename); 91 | void get_file_sections(char* filename); 92 | 93 | //TARFS file system 94 | typedef struct { 95 | char name[100]; 96 | int size; 97 | int typeflag; 98 | uint64_t addr_hdr; 99 | int par_ind; 100 | } tarfs_entry; 101 | 102 | extern tarfs_entry tarfs_fs[100]; 103 | 104 | void tarfs_init(); 105 | 106 | uint64_t open_dir(char * dir); 107 | uint64_t read_dir(uint64_t dir); 108 | file* open(char * file); 109 | int read_file(file* fd, int size, uint64_t buf); 110 | int close_file(file* file_addr); 111 | #define DIRECTORY 5 112 | #define FILE_TYPE 0 113 | 114 | #endif 115 | -------------------------------------------------------------------------------- /include/sys/timer.h: -------------------------------------------------------------------------------- 1 | #ifndef TIMER_H 2 | #define TIMER_H 3 | 4 | #include "defs.h" 5 | 6 | void init_timer(); 7 | void timer_callback(); 8 | void sleep_shell(int secs); 9 | #endif 10 | -------------------------------------------------------------------------------- /include/sys/vmem.h: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | #include 6 | #ifndef _VMEM_H 7 | #define _VMEM_H 8 | struct smap_t { 9 | uint64_t base, length; 10 | uint32_t type; 11 | }__attribute__((packed)) *smap; 12 | 13 | void init_phy_mem(uint32_t*, void* , char*); 14 | #endif 15 | -------------------------------------------------------------------------------- /include/syscall.h: -------------------------------------------------------------------------------- 1 | #ifndef _SYSCALL_H 2 | #define _SYSCALL_H 3 | 4 | #include 5 | 6 | #define SYSCALL_PROTO(n) static __inline uint64_t __syscall#n 7 | #define T_SYSCALL 128 8 | 9 | static __inline uint64_t __syscall0(uint64_t n) { 10 | 11 | uint64_t val; 12 | __asm volatile("movq %1,%%rax;" 13 | "int $0x80;" 14 | "movq %%rax,%0;" 15 | :"=r"(val) 16 | :"r"(n) 17 | :"rbp","rcx","rbx","rdx","rsi","rdi","r12","r11","r10","r9","r8","r13","r14","r15"//Clobberred registers 18 | ); 19 | return val; 20 | 21 | } 22 | 23 | static __inline uint64_t __syscall1(uint64_t n, uint64_t a1) { 24 | /* uint64_t res; 25 | __asm__ volatile ("int %1"\ 26 | :"=a"(res)\ 27 | :"i"(T_SYSCALL),"0"(n),"b"((uint64_t)(a1))\ 28 | :"rax","rbx"); 29 | return res; 30 | */ 31 | uint64_t val; 32 | __asm volatile("movq %1,%%rax;" 33 | "movq %2,%%rbx;" 34 | "int $0x80;" 35 | "movq %%rax,%0;" 36 | :"=r"(val) 37 | :"r"(n),"r"(a1) 38 | :"rbp","rcx","rdx","rsi","rdi","r12","r11","r10","r9","r8","r13","r14","r15"//Clobberred registers 39 | ); 40 | return val; 41 | 42 | } 43 | 44 | static __inline uint64_t __syscall2(uint64_t n, uint64_t a1, uint64_t a2) { 45 | uint64_t res; 46 | __asm__ volatile ("int %1"\ 47 | :"=a"(res)\ 48 | :"i"(T_SYSCALL),"0"(n) ,"b"((uint64_t)(a1)),"c"((uint64_t)(a2))\ 49 | :"cc","memory"); 50 | return res; 51 | } 52 | 53 | static __inline uint64_t __syscall3(uint64_t n, uint64_t a1, uint64_t a2, uint64_t a3) { 54 | uint64_t res; 55 | __asm__ volatile ("int %1"\ 56 | :"=a"(res)\ 57 | :"i"(T_SYSCALL),"0"(n),"b"((uint64_t)(a1)),"c"((uint64_t)(a2)),"d"((uint64_t)(a3))\ 58 | :"cc","memory"); 59 | return res; 60 | } 61 | 62 | static __inline uint64_t __syscall4(uint64_t n, uint64_t a1, uint64_t a2, uint64_t a3, uint64_t a4) { 63 | uint64_t res; 64 | __asm__ volatile ("int %1"\ 65 | :"=a"(res)\ 66 | :"i"(T_SYSCALL),"0" (n),"b"((uint64_t)(a1)),"c"((uint64_t)(a2)),"d"((uint64_t)(a3)),"D"((uint64_t)(a4))\ 67 | :"cc","memory"); 68 | return res; 69 | } 70 | 71 | 72 | /* 73 | SYSCALL_PROTO(0)(uint64_t n) { 74 | uint64_t __res; 75 | __asm__ volatile ("int %1"\ 76 | :"=a"(__res)\ 77 | :"i"(T_SYSCALL),"0"(n)\ 78 | :"cc","memory"); 79 | return __res; 80 | } 81 | 82 | SYSCALL_PROTO(1)(uint64_t n, uint64_t a1) { 83 | uint64_t __res; 84 | __asm__ volatile ("int %1"\ 85 | :"=a"(__res)\ 86 | :"i"(T_SYSCALL),"0"(n),"b"((uint64_t)(a1))\ 87 | :"cc","memory"); 88 | return __res; 89 | } 90 | 91 | SYSCALL_PROTO(2)(uint64_t n, uint64_t a1, uint64_t a2) { 92 | uint64_t __res; 93 | __asm__ volatile ("int %1"\ 94 | :"=a"(__res)\ 95 | :"i"(T_SYSCALL),"0"(n) ,"b"((uint64_t)(a1)),"c"((uint64_t)(a2))\ 96 | :"cc","memory"); 97 | return __res; 98 | } 99 | 100 | SYSCALL_PROTO(3)(uint64_t n, uint64_t a1, uint64_t a2, uint64_t a3) { 101 | uint64_t __res; 102 | __asm__ volatile ("int %1"\ 103 | :"=a"(__res)\ 104 | :"i"(T_SYSCALL),"0"(n),"b"((uint64_t)(a1)),"c"((uint64_t)(a2)),"d"((uint64_t)(a3))\ 105 | :"cc","memory"); 106 | return __res; 107 | } 108 | 109 | SYSCALL_PROTO(4)(uint64_t n, uint64_t a1, uint64_t a2, uint64_t a3, uint64_t a4) { 110 | uint64_t __res; 111 | __asm__ volatile ("int %1"\ 112 | :"=a"(__res)\ 113 | :"i"(T_SYSCALL),"0" (n),"b"((uint64_t)(a1)),"c"((uint64_t)(a2)),"d"((uint64_t)(a3)),"D"((uint64_t)(a4))\ 114 | :"cc","memory"); 115 | return __res; 116 | } 117 | */ 118 | //#endif 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 | #endif 148 | -------------------------------------------------------------------------------- /include/utils.h: -------------------------------------------------------------------------------- 1 | #ifndef UTILS_H 2 | #define UTILS_H 3 | 4 | long stoi(const char *s); 5 | uint64_t power (uint64_t x, int e); 6 | uint64_t octalToDecimal(uint64_t octal); 7 | #endif 8 | 9 | -------------------------------------------------------------------------------- /initiate_superblock: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/rajesh5310/SBUnix/441268cc45d5f133ca9cbe9566d462320303d86e/initiate_superblock -------------------------------------------------------------------------------- /initiate_superblock.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | typedef struct { 5 | char fs_type[10]; 6 | int size; 7 | int magic_no; 8 | int inode_start_sector; 9 | int data_start_sector; 10 | int free_inode_block[4]; 11 | int free_data_block[1272]; 12 | } superblock; 13 | 14 | int main() 15 | { 16 | superblock f; 17 | f.fs_type[0] = 'H'; 18 | f.fs_type[1] = 'i'; 19 | f.fs_type[2] = 'p'; 20 | f.fs_type[3] = 'H'; 21 | f.fs_type[4] = 'o'; 22 | f.fs_type[5] = 'p'; 23 | f.fs_type[6] = 'F'; 24 | f.fs_type[7] = 'F'; 25 | f.fs_type[8] = '\0'; 26 | f.size = 20971520; 27 | f.magic_no = 2014; 28 | f.inode_start_sector = 23; 29 | f.data_start_sector = 191; 30 | f.free_inode_block[0] = 0; 31 | f.free_inode_block[1] = 0; 32 | f.free_inode_block[2] = 0; 33 | f.free_inode_block[3] = 0; 34 | int i ; 35 | for(i = 0 ; i < 1272 ; i++) 36 | { 37 | f.free_data_block[i] = 0; 38 | } 39 | 40 | FILE * fp; 41 | fp = fopen ("superblock", "w+"); 42 | fwrite(&f, sizeof(superblock), 1, fp); 43 | 44 | fclose(fp); 45 | 46 | /*superblock rs; 47 | fp = fopen ("superblock", "r"); 48 | fread(&rs, sizeof(superblock), 1, fp); 49 | 50 | fclose(fp); 51 | printf("\n%d", rs.size);*/ 52 | 53 | } 54 | -------------------------------------------------------------------------------- /kernel: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/rajesh5310/SBUnix/441268cc45d5f133ca9cbe9566d462320303d86e/kernel -------------------------------------------------------------------------------- /ld/ld.c: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/rajesh5310/SBUnix/441268cc45d5f133ca9cbe9566d462320303d86e/ld/ld.c -------------------------------------------------------------------------------- /libc/execvpe.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | int execvpe (const char *filename, char *const argv[], char *const env[]) 5 | { 6 | return __syscall3(10, (uint64_t)filename, (uint64_t)argv, (uint64_t)env); 7 | } 8 | -------------------------------------------------------------------------------- /libc/exit.c: -------------------------------------------------------------------------------- 1 | 2 | #include 3 | #include 4 | 5 | void exit(int status) { 6 | __syscall1(5, status); 7 | } 8 | 9 | -------------------------------------------------------------------------------- /libc/fork.c: -------------------------------------------------------------------------------- 1 | 2 | #include 3 | #include 4 | #include 5 | 6 | pid_t fork() 7 | { 8 | uint64_t a=__syscall0(4); 9 | //__syscall0(4); 10 | //printf("[print=%d]",a); 11 | //return __syscall0(4); 12 | return a; 13 | } 14 | 15 | 16 | -------------------------------------------------------------------------------- /libc/getpid.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | pid_t getpid() 5 | { 6 | return __syscall0(8); 7 | } 8 | -------------------------------------------------------------------------------- /libc/getppid.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | pid_t getppid() 5 | { 6 | return __syscall0(9); 7 | } 8 | -------------------------------------------------------------------------------- /libc/idt.c: -------------------------------------------------------------------------------- 1 | /* @name : idt.c 2 | * @author : rgolani, abmishra, skandalamsri 3 | * @last updated date : 25th September, 2013 4 | */ 5 | 6 | #include 7 | #include 8 | #include 9 | #include 10 | static void init_idt(); 11 | 12 | /* 13 | * This method sets the entry for the secified interrupt and maps it to passed method 14 | */ 15 | static void idt_set_gate(int,uint16_t, unsigned , unsigned , unsigned , uint64_t); 16 | 17 | idt_entry_t idt_entries[256]; 18 | idt_ptr_t idt_ptr; 19 | 20 | void reload_idt() 21 | { 22 | init_idt(); 23 | }; 24 | 25 | // Write len copies of val into dest. 26 | 27 | 28 | extern void isr0(); //DUMMY_INTERRUPT(0) 29 | extern void isr32(); 30 | extern void isr33(); 31 | 32 | void load_irq() 33 | { 34 | outb(0x20, 0x11); 35 | outb(0xA0, 0x11); 36 | outb(0x21, 0x20); 37 | outb(0xA1, 0x28); 38 | outb(0x21, 0x04); 39 | outb(0xA1, 0x02); 40 | outb(0x21, 0x01); 41 | outb(0xA1, 0x01); 42 | outb(0x21, 0x0); 43 | outb(0xA1, 0x0); 44 | 45 | idt_set_gate(32,8, 0, 0x8E, 0, ((uint64_t)isr32)); 46 | idt_set_gate(33,8, 0, 0x8E, 0, ((uint64_t)isr33)); 47 | __asm__("sti"); 48 | } 49 | 50 | static void init_idt() 51 | { 52 | memset(&idt_entries, 0, sizeof(idt_entry_t ) * 256); 53 | idt_ptr.limit = sizeof(idt_entry_t) * 256 - 1; 54 | idt_ptr.base = (uint64_t)&idt_entries; 55 | 56 | idt_set_gate(0,8, 0, 0x0e, 0, ((uint64_t)&isr0)); 57 | _x86_64_asm_igdt(&idt_ptr); 58 | }; 59 | 60 | static void idt_set_gate(int num,uint16_t selector, unsigned ist, unsigned type, unsigned dpl, uint64_t offset) 61 | { 62 | idt_entries[num].offset_low = (offset) & 0xFFFF; 63 | idt_entries[num].selector = selector; 64 | idt_entries[num].ist = ist; 65 | idt_entries[num].reserved0 = 0; 66 | idt_entries[num].type = type; 67 | idt_entries[num].zero = 0; 68 | idt_entries[num].dpl = dpl; 69 | idt_entries[num].p = 1; 70 | idt_entries[num].offset_mid = (offset >> 16) & 0xFFFF; 71 | idt_entries[num].offset_high = (offset >> 32) & 0xFFFFFFFF; 72 | }; 73 | -------------------------------------------------------------------------------- /libc/idt.s: -------------------------------------------------------------------------------- 1 | # @name : idt.s 2 | # @author : rgolani, abmishra, skandalamsri 3 | # @last updated date : 25th September, 2013 4 | 5 | .text 6 | 7 | ###### 8 | # load a new IDT 9 | # parameter 1: address of idtr 10 | .global _x86_64_asm_igdt 11 | _x86_64_asm_igdt: 12 | lidt (%rdi) 13 | retq # far-return to new cs descriptor ( the retq below ) 14 | 15 | -------------------------------------------------------------------------------- /libc/isr.c: -------------------------------------------------------------------------------- 1 | /* @name : isr.c 2 | * @author : rgolani, abmishra, skandalamsri 3 | * @last updated date : 25th September, 2013 4 | */ 5 | 6 | #include 7 | #include 8 | #include 9 | #include 10 | #include 11 | #include 12 | 13 | /* 14 | * Structure that returns parameters from the ISR routines 15 | */ 16 | struct isr_stack_frame 17 | { 18 | uint64_t rip; 19 | uint64_t cs; 20 | uint64_t rflags; 21 | uint64_t rsp; 22 | uint64_t ss; 23 | }; 24 | 25 | /* 26 | * Divide-by-zero ISR handler 27 | */ 28 | void isr_handler0(struct isr_stack_frame *stack) { 29 | 30 | print("DIVIDE BY ZERO ERROR!\n"); 31 | print(" CS:0x%x\n",stack->cs); 32 | print(" RIP:0x%x\n",stack->rip); 33 | __asm__( "hlt" ); 34 | } 35 | 36 | /* 37 | * Mapped to IRQ-0. Hardware timer handler. 38 | */ 39 | void isr_handler32(struct isr_stack_frame *stack) { 40 | 41 | outb(0x20, 0x20); 42 | timer_callback(); 43 | } 44 | 45 | /* 46 | * Mapped to IRQ-1. Keyboard interrupt habdler. 47 | */ 48 | void isr_handler33(struct isr_stack_frame *stack) { 49 | 50 | outb(0x20, 0x20); 51 | handle_keyboard_irq(); 52 | } 53 | -------------------------------------------------------------------------------- /libc/isr.s: -------------------------------------------------------------------------------- 1 | # @name : isr.s 2 | # @author : rgolani, abmishra, skandalamsri 3 | # @last updated date : 25th September, 2013 4 | .text 5 | 6 | .macro set_interrupt index 7 | .global isr\index 8 | isr\index: 9 | cli 10 | pushq %rax 11 | pushq %rcx 12 | pushq %rdx 13 | pushq %rsi 14 | pushq %rdi 15 | pushq %r8 16 | pushq %r9 17 | pushq %r10 18 | pushq %r11 19 | movq %rsp,%rdi 20 | addq $72, %rdi 21 | call isr_handler\index 22 | popq %r11 23 | popq %r10 24 | popq %r9 25 | popq %r8 26 | popq %rdi 27 | popq %rsi 28 | popq %rdx 29 | popq %rcx 30 | popq %rax 31 | sti 32 | iretq 33 | .endm 34 | 35 | set_interrupt 0 36 | set_interrupt 32 37 | set_interrupt 33 38 | -------------------------------------------------------------------------------- /libc/keyboard.c: -------------------------------------------------------------------------------- 1 | /* @name : keyboard.c 2 | * @author : rgolani, abmishra, skandalamsri 3 | * @last updated date : 25th September, 2013 4 | */ 5 | 6 | #include 7 | #include 8 | #include 9 | #include 10 | 11 | /* 12 | * Routine that handles keyboard interrupt 13 | */ 14 | void handle_keyboard_irq() { 15 | 16 | static uint8_t shift = 0; 17 | static uint8_t ctrl= 0; 18 | 19 | // Read scan from port 0x60 20 | uint8_t b = port_inb(0x60); 21 | 22 | //Detect if shift is pressed 23 | switch(b) 24 | { 25 | case KBCSC_LEFTSHIFT: 26 | shift |= 1; 27 | break; 28 | case KBCSC_RIGHTSHIFT: 29 | shift |= 2; 30 | break; 31 | case (KBCSC_LEFTSHIFT | 0x80): 32 | shift &= ~1; 33 | break; 34 | case (KBCSC_RIGHTSHIFT | 0x80): 35 | shift &= ~2; 36 | /*case (KBCSC_LEFTCTRL | 0x80): 37 | ctrl |= 1; 38 | break; 39 | case (KBCSC_LEFTCTRL): 40 | ctrl |= 1; 41 | break;*/ 42 | /*case (KBCSC_RIGHTCTRL | 0x80): 43 | shift &= ~2;*/ 44 | 45 | default: 46 | break; 47 | } 48 | 49 | //Call method that converts scan code to character 50 | uint8_t c = get_tochar(b, shift, ctrl); 51 | if(c!=0) 52 | { 53 | print("%c", c); 54 | print_line(1,23, "%c", c); 55 | } 56 | 57 | } 58 | 59 | /* 60 | * Method that prints character as per the shift value 61 | */ 62 | static uint8_t char_shift_adjust(uint8_t c, uint8_t shift) { 63 | 64 | return c + (shift ? ('A'-'a') : 0); 65 | } 66 | 67 | /* 68 | * Method that converts scan code to character 69 | */ 70 | uint8_t get_tochar(uint8_t b, uint8_t shift , uint8_t ctrl) { 71 | 72 | 73 | if(b==KBCSC_SPACE) 74 | return ' '; 75 | 76 | if(b==KBCSC_RETURN) 77 | print("\n"); 78 | 79 | if(b==KBCSC_BACKSPACE) 80 | print_backspace(); 81 | 82 | if((b >= KBCSC_Q) && (b <= KBCSC_P)) 83 | { 84 | return char_shift_adjust("qwertyuiop"[b-KBCSC_Q], shift); 85 | } 86 | 87 | 88 | if((b >= KBCSC_A) && (b <= KBCSC_L)) 89 | return char_shift_adjust("asdfghjkl"[b-KBCSC_A], shift); 90 | 91 | if((b >= KBCSC_Z) && (b <= KBCSC_M)) 92 | return char_shift_adjust("zxcvbnm"[b-KBCSC_Z], shift); 93 | 94 | if((b >= KBCSC_1) && (b <= KBCSC_9)) { 95 | if(shift) 96 | return "!@#$%^&*("[b-KBCSC_1]; 97 | return '1' + b-KBCSC_1; 98 | } 99 | 100 | if((b >= KBCSC_OPEN_CURL) && (b <= KBCSC_CLOSE_CURL)) { 101 | if(shift) 102 | return "{}"[b-KBCSC_OPEN_CURL]; 103 | return "[]"[b-KBCSC_OPEN_CURL]; 104 | } 105 | 106 | if((b >= KBCSC_COLON) && (b <= KBCSC_TILDE)) { 107 | if(shift) 108 | return ":\"~"[b-KBCSC_COLON]; 109 | return ";'`"[b-KBCSC_COLON]; 110 | } 111 | 112 | if((b >= KBCSC_COMMA) && (b <= KBCSC_SLASH)) { 113 | if(shift) 114 | return "<>?"[b-KBCSC_COMMA]; 115 | return ",./"[b-KBCSC_COMMA]; 116 | } 117 | 118 | 119 | if((b >= KBCSC_UNDERSCORE_MINUS) && (b <= KBCSC_EQUALS_PLUS)) { 120 | if(shift) 121 | return "_+"[b-KBCSC_UNDERSCORE_MINUS]; 122 | return "-="[b-KBCSC_UNDERSCORE_MINUS]; 123 | } 124 | 125 | 126 | if(b == KBCSC_OR) 127 | return shift ? '|' : '\\'; 128 | 129 | 130 | if(b == KBCSC_0) 131 | return shift ? ')' : '0'; 132 | 133 | return 0; 134 | } 135 | 136 | -------------------------------------------------------------------------------- /libc/malloc.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | void* malloc(uint64_t size) 5 | { 6 | return (void *)(__syscall1(7, size)); 7 | } 8 | -------------------------------------------------------------------------------- /libc/open.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | file* open(char* file_name) { 5 | uint64_t addr = __syscall1(14, (uint64_t)file_name); 6 | if(addr == 0) 7 | return NULL; 8 | return (file *) addr; 9 | } 10 | -------------------------------------------------------------------------------- /libc/opendir.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | 5 | uint64_t opendir(char* dir) { 6 | uint64_t addr = __syscall1(12, (uint64_t)dir); 7 | return addr; 8 | } 9 | -------------------------------------------------------------------------------- /libc/ports.c: -------------------------------------------------------------------------------- 1 | /* @name : ports.c 2 | * @author : rgolani, abmishra, skandalamsri 3 | * @last updated date : 25th September, 2013 4 | */ 5 | #include 6 | 7 | inline void outb(uint16_t port, uint8_t data) { 8 | 9 | __asm__ __volatile( "outb %0, %1;" 10 | : /* void */ 11 | : "a" (data), "d" (port)); 12 | 13 | } 14 | 15 | void memset(void *dest, uint8_t val, uint32_t len) 16 | { 17 | uint8_t *temp = (uint8_t *)dest; 18 | for ( ; len != 0; len--) *temp++ = val; 19 | } 20 | 21 | inline uint8_t port_inb(uint16_t port) { 22 | 23 | uint8_t ret; 24 | __asm__ __volatile__( "inb %1, %0;" 25 | :"=a" (ret) 26 | :"d" (port) ); 27 | return ret; 28 | } 29 | -------------------------------------------------------------------------------- /libc/printf.c: -------------------------------------------------------------------------------- 1 | 2 | #include 3 | #include 4 | 5 | #define va_start(v,l) __builtin_va_start(v,l) 6 | #define va_arg(v,l) __builtin_va_arg(v,l) 7 | #define va_end(v) __builtin_va_end(v) 8 | //#define va_copy(d,s) __builtin_va_copy(d,s) 9 | typedef __builtin_va_list va_list; 10 | 11 | 12 | // Will return the length of the string 13 | unsigned int strlen(const char *str) 14 | { 15 | int i = 0; 16 | while(str[i] != '\0') 17 | i++; 18 | return i; 19 | } 20 | 21 | // Will set the int array to 0's 22 | unsigned int int_array_reset(int array[], int cnt) 23 | { 24 | int i = 0; 25 | for(i = 0; i < cnt; i++) { 26 | array[i] = 0; 27 | } 28 | return (1); 29 | } 30 | 31 | // Will set the char array to '\0' 32 | unsigned int char_array_reset(char array[], int cnt) 33 | { 34 | int i = 0; 35 | for(i = 0; i < cnt; i++) { 36 | array[i] = '\0'; 37 | } 38 | return (1); 39 | } 40 | 41 | // will spit out the integer at the given line number 42 | unsigned int printf_integer(int message, char* str) // the message and the line # 43 | { 44 | // char *vidmem = (char *) VIDMEM; 45 | unsigned int i = 0; 46 | 47 | int digits[12]; 48 | // char char_digits[12]; 49 | unsigned int remainder = 0; 50 | unsigned int quotient = message; 51 | int cnt = 0; 52 | //int initial_p = 0; 53 | // int j = 0; 54 | 55 | int_array_reset(digits, 12); 56 | 57 | while(quotient >= 10) { 58 | remainder = (int) (quotient % 10); 59 | quotient = (int) (quotient / 10); 60 | 61 | digits[cnt] = remainder; 62 | cnt++; 63 | } 64 | digits[cnt] = quotient; 65 | while(cnt >= 0) { 66 | *str++ = digits[cnt] + 48; 67 | // vidmem[i] = digits[cnt] + 48; 68 | // char_digits[j++] = digits[cnt]; 69 | cnt--; 70 | i++; 71 | // vidmem[i] = WHITE_TXT; 72 | // i++; 73 | } 74 | *str++ = '\0'; 75 | return i; 76 | // return char_digits; 77 | } 78 | 79 | // will spit out the hexadecimal at the given line number 80 | unsigned int printf_hexadecimal(unsigned long message, char* str) // the message and the line # 81 | { 82 | // char *vidmem = (char *) VIDMEM; 83 | unsigned int i = 0; 84 | 85 | int cnt = 0; 86 | // int initial_p = 0; 87 | char result[8]; 88 | char hex[] = "0123456789abcdef"; 89 | unsigned long quotient = message; 90 | 91 | char_array_reset(result, 8); 92 | 93 | while(quotient > 0) { 94 | result[cnt++] = hex[(quotient % 16)]; 95 | quotient = (quotient / 16); 96 | } 97 | // vidmem[i++] = '0'; 98 | // vidmem[i++] = WHITE_TXT; 99 | // vidmem[i++] = 'x'; 100 | // vidmem[i++] = WHITE_TXT; 101 | 102 | *str++ = '0'; 103 | *str++ = 'x'; 104 | 105 | cnt--; 106 | while(cnt >= 0) 107 | { 108 | *str++ = result[cnt]; 109 | // vidmem[i++] = result[cnt]; 110 | // vidmem[i++] = WHITE_TXT; 111 | cnt--; 112 | i++; 113 | } 114 | 115 | *str++ = '\0'; 116 | return i; 117 | } 118 | 119 | /* 120 | * Handles print function 121 | */ 122 | int printf(const char *fmt, ...) 123 | { 124 | va_list args; 125 | 126 | int len = 0; 127 | // int str_len = 0; 128 | int cnt = 0; 129 | char str[1024]; 130 | char str_temp[1024]; 131 | int i = 0; 132 | 133 | va_start(args, fmt); 134 | 135 | // flush array 136 | char_array_reset(str, 1024); 137 | char_array_reset(str_temp, 1024); 138 | 139 | // TODO: write the code to print 140 | for(;*fmt;) 141 | { 142 | if(*fmt != '%') { 143 | str[cnt++] = *fmt++; 144 | continue; 145 | } 146 | 147 | fmt++; 148 | switch(*fmt) { 149 | case 'c': 150 | str[cnt++] = (unsigned char) va_arg(args, int); 151 | fmt++; 152 | continue; 153 | case 'd': 154 | i = 0; 155 | printf_integer(va_arg(args, int), str_temp); 156 | len = strlen(str_temp); 157 | while(i < len) { 158 | str[cnt++] = str_temp[i++]; 159 | } 160 | fmt++; 161 | continue; 162 | case 's': 163 | i = 0; 164 | char *str_t = (char *)va_arg(args, char *); 165 | len = strlen(str_t); 166 | while(*str_t!='\0') 167 | { 168 | str[cnt++] = *str_t++; 169 | } 170 | fmt++; 171 | continue; 172 | case 'x': 173 | i = 0; 174 | printf_hexadecimal(va_arg(args, int), str_temp); 175 | len = strlen(str_temp); 176 | while(i < len) 177 | { 178 | str[cnt++] = str_temp[i++]; 179 | } 180 | fmt++; 181 | continue; 182 | case 'p': 183 | i = 0; 184 | printf_hexadecimal(va_arg(args, unsigned long), str_temp); 185 | len = strlen(str_temp); 186 | while(i < len) 187 | { 188 | str[cnt++] = str_temp[i++]; 189 | } 190 | fmt++; 191 | continue; 192 | default: 193 | fmt++; 194 | continue; 195 | } 196 | } 197 | str[cnt] = '\0'; 198 | 199 | va_end(args); 200 | int r = 0; 201 | while(str[r] != '\0') 202 | __syscall1(2, (uint64_t)str[r++]); 203 | return 0; 204 | } 205 | -------------------------------------------------------------------------------- /libc/printf.c.bak: -------------------------------------------------------------------------------- 1 | 2 | #include 3 | #include 4 | 5 | int putchar_user(char ch); 6 | #define va_start(v,l) __builtin_va_start(v,l) 7 | #define va_arg(v,l) __builtin_va_arg(v,l) 8 | #define va_end(v) __builtin_va_end(v) 9 | //#define va_copy(d,s) __builtin_va_copy(d,s) 10 | typedef __builtin_va_list va_list; 11 | /* 12 | * Converts number of any base to string 13 | */ 14 | char * convert_to_string_user(uint64_t value, char * str, int base ) 15 | { 16 | //int length=0,start, end; 17 | int j, length = 0; 18 | //char c; 19 | char temp[30]; 20 | // Check for supported base. 21 | if ( base < 2 || base > 36 ) 22 | { 23 | *str = '\0'; 24 | return str; 25 | } 26 | 27 | // Set '-' for negative decimals. 28 | if ( value < 0 && base == 10 ) 29 | { 30 | *str++ = '-'; 31 | } 32 | // Remember where the numbers start. 33 | //low = str; 34 | // The actual conversion. 35 | do 36 | { 37 | // Modulo is negative for negative value. This trick makes abs() unnecessary. 38 | temp[length++] = "zyxwvutsrqponmlkjihgfedcba9876543210123456789abcdefghijklmnopqrstuvwxyz"[35 + value % base]; 39 | value /= base; 40 | } while ( value ); 41 | // Terminating the string. 42 | temp[length] = '\0'; 43 | for(j= length-1; j >=0 ; j--) 44 | { 45 | *str++ = temp[j]; 46 | } 47 | *str = '\0'; 48 | return str; 49 | } 50 | 51 | /* 52 | * Handles print function 53 | */ 54 | int printf(char *message, ...) 55 | { 56 | char buffer[1024]; 57 | char *buf_pointer = buffer; 58 | char* message_local; 59 | uint64_t temp_int; 60 | char temp_char; 61 | va_list vl; 62 | void *temp_m; 63 | uint64_t p_add; 64 | uint64_t count; 65 | 66 | va_start(vl, message); 67 | message_local = message; 68 | while( *message_local != 0 ) 69 | { 70 | 71 | if(*message_local != 37 && *message_local != '\n') 72 | { 73 | *buf_pointer++ = *message_local; 74 | message_local++; 75 | continue; 76 | } 77 | 78 | if(*message_local == '\n') 79 | { 80 | *buf_pointer++ = *message_local; 81 | message_local++; 82 | continue; 83 | } 84 | 85 | switch(*++message_local) 86 | { 87 | char temp_str_i[1024], *temp_str; 88 | case 's': 89 | //temp_str = NULL; 90 | temp_str = va_arg(vl, char *); 91 | while(*temp_str) { 92 | *buf_pointer++ = *temp_str; 93 | temp_str++; 94 | 95 | } 96 | message_local = message_local + 1; 97 | break; 98 | case 'd': 99 | temp_int = va_arg(vl, uint64_t); 100 | convert_to_string_user(temp_int, temp_str_i, 10); 101 | count = 0; 102 | while(temp_str_i[count]!='\0') { 103 | *buf_pointer++ = temp_str_i[count]; 104 | count++; 105 | } 106 | message_local = message_local + 1; 107 | break; 108 | case 'x': 109 | temp_str = NULL; 110 | temp_int = (uint64_t)va_arg(vl, uint64_t); 111 | convert_to_string_user(temp_int, temp_str_i, 16); 112 | count = 0; 113 | while(temp_str_i[count]!='\0') { 114 | *buf_pointer++ = temp_str_i[count]; 115 | count++; 116 | } 117 | message_local = message_local + 1; 118 | break; 119 | case 'c': 120 | temp_str = NULL; 121 | temp_char = va_arg(vl, uint64_t); 122 | temp_str = &temp_char; 123 | *buf_pointer++ = *temp_str; 124 | message_local = message_local + 1; 125 | break; 126 | case 'p': 127 | temp_str = NULL; 128 | temp_m = va_arg(vl, void *); 129 | p_add = (uint64_t) temp_m; 130 | convert_to_string_user(p_add, temp_str, 16); 131 | while(*temp_str) { 132 | *buf_pointer++ = *temp_str; 133 | temp_str++; 134 | } 135 | message_local = message_local + 1; 136 | break; 137 | } 138 | } 139 | va_end(vl); 140 | *buf_pointer = '\0'; 141 | __syscall1(2, (uint64_t)buffer); 142 | return 0; 143 | } 144 | 145 | -------------------------------------------------------------------------------- /libc/printf_color.c: -------------------------------------------------------------------------------- 1 | 2 | #include 3 | #include 4 | 5 | #define va_start(v,l) __builtin_va_start(v,l) 6 | #define va_arg(v,l) __builtin_va_arg(v,l) 7 | #define va_end(v) __builtin_va_end(v) 8 | //#define va_copy(d,s) __builtin_va_copy(d,s) 9 | typedef __builtin_va_list va_list; 10 | 11 | 12 | // Will return the length of the string 13 | unsigned int strlen2(const char *str) 14 | { 15 | int i = 0; 16 | while(str[i] != '\0') 17 | i++; 18 | return i; 19 | } 20 | 21 | // Will set the int array to 0's 22 | unsigned int int_array_reset2(int array[], int cnt) 23 | { 24 | int i = 0; 25 | for(i = 0; i < cnt; i++) { 26 | array[i] = 0; 27 | } 28 | return (1); 29 | } 30 | 31 | // Will set the char array to '\0' 32 | unsigned int char_array_reset2(char array[], int cnt) 33 | { 34 | int i = 0; 35 | for(i = 0; i < cnt; i++) { 36 | array[i] = '\0'; 37 | } 38 | return (1); 39 | } 40 | 41 | // will spit out the integer at the given line number 42 | unsigned int printf_integer2(int message, char* str) // the message and the line # 43 | { 44 | // char *vidmem = (char *) VIDMEM; 45 | unsigned int i = 0; 46 | 47 | int digits[12]; 48 | // char char_digits[12]; 49 | unsigned int remainder = 0; 50 | unsigned int quotient = message; 51 | int cnt = 0; 52 | //int initial_p = 0; 53 | // int j = 0; 54 | 55 | int_array_reset2(digits, 12); 56 | 57 | while(quotient >= 10) { 58 | remainder = (int) (quotient % 10); 59 | quotient = (int) (quotient / 10); 60 | 61 | digits[cnt] = remainder; 62 | cnt++; 63 | } 64 | digits[cnt] = quotient; 65 | while(cnt >= 0) { 66 | *str++ = digits[cnt] + 48; 67 | // vidmem[i] = digits[cnt] + 48; 68 | // char_digits[j++] = digits[cnt]; 69 | cnt--; 70 | i++; 71 | // vidmem[i] = WHITE_TXT; 72 | // i++; 73 | } 74 | *str++ = '\0'; 75 | return i; 76 | // return char_digits; 77 | } 78 | 79 | // will spit out the hexadecimal at the given line number 80 | unsigned int printf_hexadecimal2(unsigned long message, char* str) // the message and the line # 81 | { 82 | // char *vidmem = (char *) VIDMEM; 83 | unsigned int i = 0; 84 | 85 | int cnt = 0; 86 | // int initial_p = 0; 87 | char result[8]; 88 | char hex[] = "0123456789abcdef"; 89 | unsigned long quotient = message; 90 | 91 | char_array_reset2(result, 8); 92 | 93 | while(quotient > 0) { 94 | result[cnt++] = hex[(quotient % 16)]; 95 | quotient = (quotient / 16); 96 | } 97 | // vidmem[i++] = '0'; 98 | // vidmem[i++] = WHITE_TXT; 99 | // vidmem[i++] = 'x'; 100 | // vidmem[i++] = WHITE_TXT; 101 | 102 | *str++ = '0'; 103 | *str++ = 'x'; 104 | 105 | cnt--; 106 | while(cnt >= 0) 107 | { 108 | *str++ = result[cnt]; 109 | // vidmem[i++] = result[cnt]; 110 | // vidmem[i++] = WHITE_TXT; 111 | cnt--; 112 | i++; 113 | } 114 | 115 | *str++ = '\0'; 116 | return i; 117 | } 118 | 119 | /* 120 | * Handles print function 121 | */ 122 | int printf_color(const char *fmt, ...) 123 | { 124 | va_list args; 125 | 126 | int len = 0; 127 | // int str_len = 0; 128 | int cnt = 0; 129 | char str[1024]; 130 | char str_temp[1024]; 131 | int i = 0; 132 | 133 | va_start(args, fmt); 134 | 135 | // flush array 136 | char_array_reset2(str, 1024); 137 | char_array_reset2(str_temp, 1024); 138 | 139 | // TODO: write the code to print 140 | for(;*fmt;) 141 | { 142 | if(*fmt != '%') { 143 | str[cnt++] = *fmt++; 144 | continue; 145 | } 146 | 147 | fmt++; 148 | switch(*fmt) { 149 | case 'c': 150 | str[cnt++] = (unsigned char) va_arg(args, int); 151 | fmt++; 152 | continue; 153 | case 'd': 154 | i = 0; 155 | printf_integer2(va_arg(args, int), str_temp); 156 | len = strlen2(str_temp); 157 | while(i < len) { 158 | str[cnt++] = str_temp[i++]; 159 | } 160 | fmt++; 161 | continue; 162 | case 's': 163 | i = 0; 164 | char *str_t = (char *)va_arg(args, char *); 165 | len = strlen2(str_t); 166 | while(*str_t!='\0') 167 | { 168 | str[cnt++] = *str_t++; 169 | } 170 | fmt++; 171 | continue; 172 | case 'x': 173 | i = 0; 174 | printf_hexadecimal2(va_arg(args, int), str_temp); 175 | len = strlen2(str_temp); 176 | while(i < len) 177 | { 178 | str[cnt++] = str_temp[i++]; 179 | } 180 | fmt++; 181 | continue; 182 | case 'p': 183 | i = 0; 184 | printf_hexadecimal2(va_arg(args, unsigned long), str_temp); 185 | len = strlen2(str_temp); 186 | while(i < len) 187 | { 188 | str[cnt++] = str_temp[i++]; 189 | } 190 | fmt++; 191 | continue; 192 | default: 193 | fmt++; 194 | continue; 195 | } 196 | } 197 | str[cnt] = '\0'; 198 | 199 | va_end(args); 200 | int r = 0; 201 | while(str[r] != '\0') 202 | __syscall1(26, (uint64_t)str[r++]); 203 | return 0; 204 | } 205 | -------------------------------------------------------------------------------- /libc/ps.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | void ps() 5 | { 6 | __syscall0(21); 7 | } 8 | -------------------------------------------------------------------------------- /libc/read.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | 6 | int read(file *fd, int size, char* buf) { 7 | int size_read = (int)__syscall3(15, (uint64_t)fd, (uint64_t) size, (uint64_t)buf); 8 | return size_read; 9 | } 10 | -------------------------------------------------------------------------------- /libc/readdir.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | 5 | uint64_t readdir(uint64_t dir) { 6 | uint64_t ret = __syscall1(13, (uint64_t)(dir)); 7 | return ret; 8 | } 9 | -------------------------------------------------------------------------------- /libc/scanf.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | uint64_t stoi(char *s) // the message and then the line # 5 | { 6 | uint64_t i; 7 | i = 0; 8 | while(*s >= '0' && *s <= '9') 9 | { 10 | i = i * 10 + (*s - '0'); 11 | s++; 12 | } 13 | return i; 14 | } 15 | int strcmp1(const char *p, const char *q) { 16 | while (*p || *q) { 17 | if (*p != *q) 18 | return -1; 19 | p++, q++; 20 | } 21 | return 0; 22 | } 23 | 24 | 25 | 26 | 27 | void scanf(char* str, void* buf) { 28 | char temp_buf[100]; 29 | 30 | __syscall1(11, (uint64_t)temp_buf); 31 | 32 | if(strcmp1(str, "%s") == 0) 33 | { 34 | buf = (char *)buf; 35 | // printf("----- %c", temp_buf[0]); 36 | // printf("in"); 37 | //buf = temp_buf; 38 | int i = 0; 39 | while(temp_buf[i] != '\0') 40 | *(char *)buf++ = temp_buf[i++]; 41 | *(char *)buf = '\0'; 42 | // printf("%d", i); 43 | } 44 | else if(strcmp1(str, "%d") == 0) 45 | { 46 | *(int *)buf = stoi(temp_buf); 47 | } 48 | else if(strcmp1(str, "%x") == 0) 49 | { 50 | //buf = temp_buf; 51 | } 52 | 53 | } 54 | -------------------------------------------------------------------------------- /libc/seek.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | 5 | int seek(file *fd, int offset, int whence) 6 | { 7 | return __syscall3(16, (uint64_t)fd, (uint64_t)offset, (uint64_t)whence); 8 | } 9 | -------------------------------------------------------------------------------- /libc/shell_sleep.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | void shell_sleep(uint64_t ticks) 5 | { 6 | __syscall1(22, (uint64_t)ticks); 7 | } 8 | -------------------------------------------------------------------------------- /libc/shutdown.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | void shutdown(int status) { 5 | __syscall0(25); 6 | } 7 | -------------------------------------------------------------------------------- /libc/sleep.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | uint64_t sleep(uint64_t time) 5 | { 6 | return (uint64_t)__syscall1(6, time); 7 | } 8 | -------------------------------------------------------------------------------- /libc/string.c: -------------------------------------------------------------------------------- 1 | 2 | #include 3 | 4 | /*void * 5 | memset(void *v, uint64_t c, size_t n) 6 | { 7 | 8 | if (n == 0) 9 | return v; 10 | if ((uint64_t)v%4 == 0 && n%4 == 0) { 11 | c &= 0xFF; 12 | c = (c<<24)|(c<<16)|(c<<8)|c; 13 | asm volatile("cld; rep stosl\n" 14 | :: "D" (v), "a" (c), "c" (n/4) 15 | : "cc", "memory"); 16 | } else 17 | asm volatile("cld; rep stosb\n" 18 | :: "D" (v), "a" (c), "c" (n) 19 | : "cc", "memory"); 20 | return v; 21 | }*/ 22 | -------------------------------------------------------------------------------- /libc/syscall.c: -------------------------------------------------------------------------------- 1 | /* 2 | * ===================================================================================== 3 | * 4 | * Filename: syscall.c 5 | * 6 | * Description: 7 | * 8 | * Version: 1.0 9 | * Created: 11/21/2013 01:57:52 AM 10 | * Revision: none 11 | * Compiler: gcc 12 | * 13 | * Author: Er. Udit Gupta (ukg), udit.gupta@stonybrook.edu 14 | * Company: Stony Brook University 15 | * 16 | * ===================================================================================== 17 | */ 18 | 19 | 20 | #include 21 | #include 22 | #include 23 | #include 24 | #include 25 | 26 | void __exit(); 27 | void __putchar(uint64_t c); 28 | 29 | void syscall_common_handler(struct isr_stack_frame* s) { 30 | // int d=current_task->pid; 31 | // printf("Value %d\n",d); 32 | 33 | int i; 34 | // asm("\t movq %%rax,%0" : "=r"(i)); 35 | 36 | i=s->rax; 37 | 38 | if(i == 1){ 39 | //call the exit system call 40 | __exit(); 41 | } 42 | else if(i == 2){ 43 | //call for printf 44 | printf("Value %d\n",s->rbx); 45 | __putchar(s->rbx); 46 | } 47 | 48 | else if(i == 3){ 49 | //call for maybe fork 50 | } 51 | 52 | } 53 | 54 | void __putchar(uint64_t c) { 55 | printf("I AM IN PUTCHAR\n"); 56 | }; 57 | 58 | void __exit() { 59 | //first i need to implement free() for freeing memory 60 | //free vma's 61 | printf("I AM here\n"); 62 | //free mm_struct 63 | 64 | //we can free task_struct here or its better to do it in wait as mentioned in the mail thread..but bcz of time left we will do it right here 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 | -------------------------------------------------------------------------------- /libc/timer.c: -------------------------------------------------------------------------------- 1 | /* @name : timer.c 2 | * @author : rgolani, abmishra, skandalamsri 3 | * @last updated date : 25th September, 2013 4 | */ 5 | #include 6 | #include 7 | #include 8 | #include 9 | 10 | uint32_t tick = 0; 11 | 12 | /* 13 | * This method calculates the ticks and prints the time since boot on the screen 14 | */ 15 | void timer_callback() 16 | { 17 | tick++; 18 | if((tick % 100) == 0) 19 | { 20 | uint64_t seconds = tick/100; 21 | uint64_t hours = seconds/(3600); 22 | uint64_t minutes = (seconds%(3600))/60; 23 | uint64_t secs = (seconds%(3600))%60; 24 | print_line(50, 24, "Time Passed : %d:%d:%d ", hours, minutes, secs); 25 | } 26 | } 27 | 28 | /* 29 | * This method is used to initialize the timer for printinng time 30 | */ 31 | void init_timer() 32 | { 33 | 34 | int frequency = 100; 35 | uint16_t divisor = 1193180/frequency; 36 | // Send the command byte. 37 | outb(0x43, 0x36); 38 | 39 | // Divisor has to be sent byte-wise, so split here into upper/lower bytes. 40 | uint8_t l = (uint8_t)(divisor & 0xFF); 41 | uint8_t h = (uint8_t)(divisor>>8); 42 | 43 | // Send the frequency divisor. 44 | outb(0x40, l); 45 | outb(0x40, h); 46 | } 47 | -------------------------------------------------------------------------------- /libc/waitpid.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | 5 | int waitpid(int pid,int status) { 6 | return __syscall2(20,pid, status); 7 | } 8 | 9 | -------------------------------------------------------------------------------- /libc/write.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | 5 | int write(file *fd, char * buf, int size) 6 | { 7 | return __syscall3(17, (uint64_t)fd, (uint64_t)buf, (uint64_t)size); 8 | } 9 | -------------------------------------------------------------------------------- /linker.script: -------------------------------------------------------------------------------- 1 | ENTRY(boot) 2 | SECTIONS 3 | { 4 | physbase = 0x200000; 5 | kernmem = 0xffffffff80000000 + physbase; 6 | . = kernmem + SIZEOF_HEADERS; 7 | .text : { *(.text) } 8 | .rodata : { *(.rodata) } 9 | .got ALIGN(0x1000): { *(.got) *(.got.plt) } 10 | .bss ALIGN(0x1000): { *(.bss) *(COMMON) } 11 | .data : { *(.data) } 12 | } 13 | -------------------------------------------------------------------------------- /obj/bin/hello/hello.o: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/rajesh5310/SBUnix/441268cc45d5f133ca9cbe9566d462320303d86e/obj/bin/hello/hello.o -------------------------------------------------------------------------------- /obj/bin/hello/hello1.o: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/rajesh5310/SBUnix/441268cc45d5f133ca9cbe9566d462320303d86e/obj/bin/hello/hello1.o -------------------------------------------------------------------------------- /obj/bin/hello1/hello1.o: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/rajesh5310/SBUnix/441268cc45d5f133ca9cbe9566d462320303d86e/obj/bin/hello1/hello1.o -------------------------------------------------------------------------------- /obj/bin/test/test.o: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/rajesh5310/SBUnix/441268cc45d5f133ca9cbe9566d462320303d86e/obj/bin/test/test.o -------------------------------------------------------------------------------- /obj/crt/crt1.o: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/rajesh5310/SBUnix/441268cc45d5f133ca9cbe9566d462320303d86e/obj/crt/crt1.o -------------------------------------------------------------------------------- /obj/ld/ld.o: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/rajesh5310/SBUnix/441268cc45d5f133ca9cbe9566d462320303d86e/obj/ld/ld.o -------------------------------------------------------------------------------- /obj/libc/exit.o: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/rajesh5310/SBUnix/441268cc45d5f133ca9cbe9566d462320303d86e/obj/libc/exit.o -------------------------------------------------------------------------------- /obj/libc/idt.o: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/rajesh5310/SBUnix/441268cc45d5f133ca9cbe9566d462320303d86e/obj/libc/idt.o -------------------------------------------------------------------------------- /obj/libc/isr.o: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/rajesh5310/SBUnix/441268cc45d5f133ca9cbe9566d462320303d86e/obj/libc/isr.o -------------------------------------------------------------------------------- /obj/libc/keyboard.o: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/rajesh5310/SBUnix/441268cc45d5f133ca9cbe9566d462320303d86e/obj/libc/keyboard.o -------------------------------------------------------------------------------- /obj/libc/ports.o: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/rajesh5310/SBUnix/441268cc45d5f133ca9cbe9566d462320303d86e/obj/libc/ports.o -------------------------------------------------------------------------------- /obj/libc/print.o: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/rajesh5310/SBUnix/441268cc45d5f133ca9cbe9566d462320303d86e/obj/libc/print.o -------------------------------------------------------------------------------- /obj/libc/printf.o: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/rajesh5310/SBUnix/441268cc45d5f133ca9cbe9566d462320303d86e/obj/libc/printf.o -------------------------------------------------------------------------------- /obj/libc/string.o: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/rajesh5310/SBUnix/441268cc45d5f133ca9cbe9566d462320303d86e/obj/libc/string.o -------------------------------------------------------------------------------- /obj/libc/syscall.o: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/rajesh5310/SBUnix/441268cc45d5f133ca9cbe9566d462320303d86e/obj/libc/syscall.o -------------------------------------------------------------------------------- /obj/libc/timer.o: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/rajesh5310/SBUnix/441268cc45d5f133ca9cbe9566d462320303d86e/obj/libc/timer.o -------------------------------------------------------------------------------- /obj/sys/env.o: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/rajesh5310/SBUnix/441268cc45d5f133ca9cbe9566d462320303d86e/obj/sys/env.o -------------------------------------------------------------------------------- /obj/sys/fs.o: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/rajesh5310/SBUnix/441268cc45d5f133ca9cbe9566d462320303d86e/obj/sys/fs.o -------------------------------------------------------------------------------- /obj/sys/gdt.asm.o: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/rajesh5310/SBUnix/441268cc45d5f133ca9cbe9566d462320303d86e/obj/sys/gdt.asm.o -------------------------------------------------------------------------------- /obj/sys/gdt.o: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/rajesh5310/SBUnix/441268cc45d5f133ca9cbe9566d462320303d86e/obj/sys/gdt.o -------------------------------------------------------------------------------- /obj/sys/idt.asm.o: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/rajesh5310/SBUnix/441268cc45d5f133ca9cbe9566d462320303d86e/obj/sys/idt.asm.o -------------------------------------------------------------------------------- /obj/sys/idt.o: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/rajesh5310/SBUnix/441268cc45d5f133ca9cbe9566d462320303d86e/obj/sys/idt.o -------------------------------------------------------------------------------- /obj/sys/isr.asm.o: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/rajesh5310/SBUnix/441268cc45d5f133ca9cbe9566d462320303d86e/obj/sys/isr.asm.o -------------------------------------------------------------------------------- /obj/sys/isr.o: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/rajesh5310/SBUnix/441268cc45d5f133ca9cbe9566d462320303d86e/obj/sys/isr.o -------------------------------------------------------------------------------- /obj/sys/keyboard.o: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/rajesh5310/SBUnix/441268cc45d5f133ca9cbe9566d462320303d86e/obj/sys/keyboard.o -------------------------------------------------------------------------------- /obj/sys/main.o: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/rajesh5310/SBUnix/441268cc45d5f133ca9cbe9566d462320303d86e/obj/sys/main.o -------------------------------------------------------------------------------- /obj/sys/pmap.o: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/rajesh5310/SBUnix/441268cc45d5f133ca9cbe9566d462320303d86e/obj/sys/pmap.o -------------------------------------------------------------------------------- /obj/sys/ports.o: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/rajesh5310/SBUnix/441268cc45d5f133ca9cbe9566d462320303d86e/obj/sys/ports.o -------------------------------------------------------------------------------- /obj/sys/print.o: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/rajesh5310/SBUnix/441268cc45d5f133ca9cbe9566d462320303d86e/obj/sys/print.o -------------------------------------------------------------------------------- /obj/sys/process.asm.o: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/rajesh5310/SBUnix/441268cc45d5f133ca9cbe9566d462320303d86e/obj/sys/process.asm.o -------------------------------------------------------------------------------- /obj/sys/process.o: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/rajesh5310/SBUnix/441268cc45d5f133ca9cbe9566d462320303d86e/obj/sys/process.o -------------------------------------------------------------------------------- /obj/sys/string.o: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/rajesh5310/SBUnix/441268cc45d5f133ca9cbe9566d462320303d86e/obj/sys/string.o -------------------------------------------------------------------------------- /obj/sys/tarfs.o: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/rajesh5310/SBUnix/441268cc45d5f133ca9cbe9566d462320303d86e/obj/sys/tarfs.o -------------------------------------------------------------------------------- /obj/sys/timer.o: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/rajesh5310/SBUnix/441268cc45d5f133ca9cbe9566d462320303d86e/obj/sys/timer.o -------------------------------------------------------------------------------- /obj/sys/utils.o: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/rajesh5310/SBUnix/441268cc45d5f133ca9cbe9566d462320303d86e/obj/sys/utils.o -------------------------------------------------------------------------------- /obj/sys/vmem.o: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/rajesh5310/SBUnix/441268cc45d5f133ca9cbe9566d462320303d86e/obj/sys/vmem.o -------------------------------------------------------------------------------- /obj/tarfs.o: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/rajesh5310/SBUnix/441268cc45d5f133ca9cbe9566d462320303d86e/obj/tarfs.o -------------------------------------------------------------------------------- /qemu: -------------------------------------------------------------------------------- 1 | 2 | #!/bin/sh 3 | 4 | qemu-system-x86_64 -m 128 -curses -cdrom $USER.iso -hda $USER.img -gdb tcp::8888 -d cpu_reset -D ./qemu.log $1 5 | 6 | 7 | -------------------------------------------------------------------------------- /qemu.log: -------------------------------------------------------------------------------- 1 | CPU Reset (CPU 0) 2 | EAX=00000000 EBX=00000000 ECX=00000000 EDX=00000000 3 | ESI=00000000 EDI=00000000 EBP=00000000 ESP=00000000 4 | EIP=00000000 EFL=00000000 [-------] CPL=0 II=0 A20=0 SMM=0 HLT=0 5 | ES =0000 00000000 00000000 00000000 6 | CS =0000 00000000 00000000 00000000 7 | SS =0000 00000000 00000000 00000000 8 | DS =0000 00000000 00000000 00000000 9 | FS =0000 00000000 00000000 00000000 10 | GS =0000 00000000 00000000 00000000 11 | LDT=0000 00000000 00000000 00000000 12 | TR =0000 00000000 00000000 00000000 13 | GDT= 00000000 00000000 14 | IDT= 00000000 00000000 15 | CR0=00000000 CR2=00000000 CR3=00000000 CR4=00000000 16 | DR0=0000000000000000 DR1=0000000000000000 DR2=0000000000000000 DR3=0000000000000000 17 | DR6=0000000000000000 DR7=0000000000000000 18 | CCS=00000000 CCD=00000000 CCO=DYNAMIC 19 | EFER=0000000000000000 20 | FCW=0000 FSW=0000 [ST=0] FTW=ff MXCSR=00000000 21 | FPR0=0000000000000000 0000 FPR1=0000000000000000 0000 22 | FPR2=0000000000000000 0000 FPR3=0000000000000000 0000 23 | FPR4=0000000000000000 0000 FPR5=0000000000000000 0000 24 | FPR6=0000000000000000 0000 FPR7=0000000000000000 0000 25 | XMM00=00000000000000000000000000000000 XMM01=00000000000000000000000000000000 26 | XMM02=00000000000000000000000000000000 XMM03=00000000000000000000000000000000 27 | XMM04=00000000000000000000000000000000 XMM05=00000000000000000000000000000000 28 | XMM06=00000000000000000000000000000000 XMM07=00000000000000000000000000000000 29 | CPU Reset (CPU 0) 30 | EAX=00000000 EBX=00000000 ECX=00000000 EDX=00000663 31 | ESI=00000000 EDI=00000000 EBP=00000000 ESP=00000000 32 | EIP=0000fff0 EFL=00000002 [-------] CPL=0 II=0 A20=1 SMM=0 HLT=0 33 | ES =0000 00000000 0000ffff 00009300 34 | CS =f000 ffff0000 0000ffff 00009b00 35 | SS =0000 00000000 0000ffff 00009300 36 | DS =0000 00000000 0000ffff 00009300 37 | FS =0000 00000000 0000ffff 00009300 38 | GS =0000 00000000 0000ffff 00009300 39 | LDT=0000 00000000 0000ffff 00008200 40 | TR =0000 00000000 0000ffff 00008b00 41 | GDT= 00000000 0000ffff 42 | IDT= 00000000 0000ffff 43 | CR0=60000010 CR2=00000000 CR3=00000000 CR4=00000000 44 | DR0=0000000000000000 DR1=0000000000000000 DR2=0000000000000000 DR3=0000000000000000 45 | DR6=00000000ffff0ff0 DR7=0000000000000400 46 | CCS=00000000 CCD=00000000 CCO=DYNAMIC 47 | EFER=0000000000000000 48 | FCW=037f FSW=0000 [ST=0] FTW=00 MXCSR=00001f80 49 | FPR0=0000000000000000 0000 FPR1=0000000000000000 0000 50 | FPR2=0000000000000000 0000 FPR3=0000000000000000 0000 51 | FPR4=0000000000000000 0000 FPR5=0000000000000000 0000 52 | FPR6=0000000000000000 0000 FPR7=0000000000000000 0000 53 | XMM00=00000000000000000000000000000000 XMM01=00000000000000000000000000000000 54 | XMM02=00000000000000000000000000000000 XMM03=00000000000000000000000000000000 55 | XMM04=00000000000000000000000000000000 XMM05=00000000000000000000000000000000 56 | XMM06=00000000000000000000000000000000 XMM07=00000000000000000000000000000000 57 | -------------------------------------------------------------------------------- /qemud: -------------------------------------------------------------------------------- 1 | 2 | #!/bin/sh 3 | 4 | make clean && make 5 | qemu-system-x86_64 -m 128 -curses -cdrom $USER.iso -hda $USER.img -gdb tcp::$1 -d cpu_reset -D ./qemu.log $2 6 | 7 | 8 | -------------------------------------------------------------------------------- /qemuhdd: -------------------------------------------------------------------------------- 1 | 2 | #!/bin/sh 3 | 4 | qemu-system-x86_64 -curses -cdrom $USER.iso -drive id=disk,file=hdd.img,if=none -device ahci,id=ahci -device ide-drive,drive=disk,bus=ahci.0 -gdb tcp::$1 5 | 6 | 7 | -------------------------------------------------------------------------------- /rajesh.iso: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/rajesh5310/SBUnix/441268cc45d5f133ca9cbe9566d462320303d86e/rajesh.iso -------------------------------------------------------------------------------- /rgolani.iso: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/rajesh5310/SBUnix/441268cc45d5f133ca9cbe9566d462320303d86e/rgolani.iso -------------------------------------------------------------------------------- /rgolani.tgz: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/rajesh5310/SBUnix/441268cc45d5f133ca9cbe9566d462320303d86e/rgolani.tgz -------------------------------------------------------------------------------- /rgolani.tgz.gpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/rajesh5310/SBUnix/441268cc45d5f133ca9cbe9566d462320303d86e/rgolani.tgz.gpg -------------------------------------------------------------------------------- /rootfs/aaatest: -------------------------------------------------------------------------------- 1 | test -------------------------------------------------------------------------------- /rootfs/bin/.empty: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/rajesh5310/SBUnix/441268cc45d5f133ca9cbe9566d462320303d86e/rootfs/bin/.empty -------------------------------------------------------------------------------- /rootfs/bin/hello: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/rajesh5310/SBUnix/441268cc45d5f133ca9cbe9566d462320303d86e/rootfs/bin/hello -------------------------------------------------------------------------------- /rootfs/bin/hello1: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/rajesh5310/SBUnix/441268cc45d5f133ca9cbe9566d462320303d86e/rootfs/bin/hello1 -------------------------------------------------------------------------------- /rootfs/bin/test: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/rajesh5310/SBUnix/441268cc45d5f133ca9cbe9566d462320303d86e/rootfs/bin/test -------------------------------------------------------------------------------- /rootfs/boot/beastie.4th: -------------------------------------------------------------------------------- 1 | \ Copyright (c) 2003 Scott Long 2 | \ Copyright (c) 2003 Aleksander Fafula 3 | \ Copyright (c) 2006-2011 Devin Teske 4 | \ All rights reserved. 5 | \ 6 | \ Redistribution and use in source and binary forms, with or without 7 | \ modification, are permitted provided that the following conditions 8 | \ are met: 9 | \ 1. Redistributions of source code must retain the above copyright 10 | \ notice, this list of conditions and the following disclaimer. 11 | \ 2. Redistributions in binary form must reproduce the above copyright 12 | \ notice, this list of conditions and the following disclaimer in the 13 | \ documentation and/or other materials provided with the distribution. 14 | \ 15 | \ THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND 16 | \ ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 17 | \ IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 18 | \ ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE 19 | \ FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 20 | \ DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 21 | \ OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 22 | \ HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 23 | \ LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 24 | \ OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 25 | \ SUCH DAMAGE. 26 | \ 27 | \ $FreeBSD: release/9.1.0/sys/boot/forth/beastie.4th 222417 2011-05-28 08:50:38Z julian $ 28 | 29 | marker task-beastie.4th 30 | 31 | variable logoX 32 | variable logoY 33 | 34 | \ Initialize logo placement to defaults 35 | 4 logoX ! 36 | 4 logoY ! 37 | 38 | : cse506-logo ( x y -- ) \ CSE506 39 | 40 | 2dup at-xy ." " 1+ 41 | 2dup at-xy ." " 1+ 42 | 2dup at-xy ." " 1+ 43 | 2dup at-xy ." " 1+ 44 | 2dup at-xy ." _________ ____________________" 1+ 45 | 2dup at-xy ." \_ ___ \ / _____/\_ _____/" 1+ 46 | 2dup at-xy ." / \ \/ \_____ \ | __)_ " 1+ 47 | 2dup at-xy ." \ \____/ \ | \" 1+ 48 | 2dup at-xy ." \______ /_______ //_______ /" 1+ 49 | 2dup at-xy ." \/ \/ \/ " 1+ 50 | 2dup at-xy ." ._______________ ________" 1+ 51 | 2dup at-xy ." | ____/\ _ \ / _____/" 1+ 52 | 2dup at-xy ." |____ \ / /_\ \/ __ \ " 1+ 53 | 2dup at-xy ." / \\ \_/ \ |__\ \" 1+ 54 | 2dup at-xy ." /______ / \_____ /\_____ /" 1+ 55 | 2dup at-xy ." \/ \/ \/ " 1+ 56 | at-xy ." " 57 | 58 | \ Put the cursor back at the bottom 59 | 0 22 at-xy 60 | ; 61 | 62 | : beastie-logo ( x y -- ) \ color BSD mascot (19 rows x 34 columns) 63 | 64 | 2dup at-xy ." , ," 1+ 65 | 2dup at-xy ." /( )`" 1+ 66 | 2dup at-xy ." \ \___ / |" 1+ 67 | 2dup at-xy ." /- _ `-/ '" 1+ 68 | 2dup at-xy ." (/\/ \ \ /\" 1+ 69 | 2dup at-xy ." / / | ` \" 1+ 70 | 2dup at-xy ." O O ) / |" 1+ 71 | 2dup at-xy ." `-^--'`< '" 1+ 72 | 2dup at-xy ." (_.) _ ) /" 1+ 73 | 2dup at-xy ." `.___/` /" 1+ 74 | 2dup at-xy ." `-----' /" 1+ 75 | 2dup at-xy ." <----. __ / __ \" 1+ 76 | 2dup at-xy ." <----|====O)))==) \) /====|" 1+ 77 | 2dup at-xy ." <----' `--' `.__,' \" 1+ 78 | 2dup at-xy ." | |" 1+ 79 | 2dup at-xy ." \ / /\" 1+ 80 | 2dup at-xy ." ______( (_ / \______/" 1+ 81 | 2dup at-xy ." ,' ,-----' |" 1+ 82 | at-xy ." `--{__________)" 83 | 84 | \ Put the cursor back at the bottom 85 | 0 25 at-xy 86 | ; 87 | 88 | : orb-logo ( x y -- ) \ color Orb mascot (15 rows x 30 columns) 89 | 90 | 3 + \ beastie adjustment (see `fbsdbw-logo' comments above) 91 | 92 | 2dup at-xy ." ``` `" 1+ 93 | 2dup at-xy ." s` `.....---.......--.``` -/" 1+ 94 | 2dup at-xy ." +o .--` /y:` +." 1+ 95 | 2dup at-xy ." yo`:. :o `+-" 1+ 96 | 2dup at-xy ." y/ -/` -o/" 1+ 97 | 2dup at-xy ." .- ::/sy+:." 1+ 98 | 2dup at-xy ." / `-- /" 1+ 99 | 2dup at-xy ." `: :`" 1+ 100 | 2dup at-xy ." `: :`" 1+ 101 | 2dup at-xy ." / /" 1+ 102 | 2dup at-xy ." .- -." 1+ 103 | 2dup at-xy ." -- -." 1+ 104 | 2dup at-xy ." `:` `:`" 1+ 105 | 2dup at-xy ." .-- `--." 1+ 106 | at-xy ." .---.....----." 107 | 108 | \ Put the cursor back at the bottom 109 | 0 25 at-xy 110 | ; 111 | 112 | \ This function draws any number of beastie logos at (loader_logo_x, 113 | \ loader_logo_y) if defined, else (46,4) (to the right of the menu). To choose 114 | \ your beastie, set the variable `loader_logo' to the respective logo name. 115 | \ 116 | \ Currently available: 117 | \ 118 | \ NAME DESCRIPTION 119 | \ beastie Color ``Helper Daemon'' mascot (19 rows x 34 columns) 120 | \ beastiebw B/W ``Helper Daemon'' mascot (19 rows x 34 columns) 121 | \ fbsdbw "FreeBSD" logo in B/W (13 rows x 21 columns) 122 | \ orb Color ``Orb'' mascot (15 rows x 30 columns) 123 | \ orbbw B/W ``Orb'' mascot (15 rows x 32 columns) (default) 124 | \ 125 | \ NOTE: Setting `loader_logo' to an undefined value (such as "none") will 126 | \ prevent beastie from being drawn. 127 | \ 128 | : draw-beastie ( -- ) \ at (loader_logo_x,loader_logo_y), else (46,4) 129 | 130 | s" loader_logo" getenv dup -1 = if 131 | drop exit 132 | then 133 | 134 | 2dup s" cse506" compare-insensitive 0= if 135 | logoX @ logoY @ beastie-logo 136 | 2drop exit 137 | then 138 | 2dup s" beastie" compare-insensitive 0= if 139 | logoX @ logoY @ beastie-logo 140 | 2drop exit 141 | then 142 | 2dup s" orb" compare-insensitive 0= if 143 | logoX @ logoY @ orb-logo 144 | 2drop exit 145 | then 146 | 147 | 2drop 148 | ; 149 | -------------------------------------------------------------------------------- /rootfs/boot/boot: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/rajesh5310/SBUnix/441268cc45d5f133ca9cbe9566d462320303d86e/rootfs/boot/boot -------------------------------------------------------------------------------- /rootfs/boot/boot0: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/rajesh5310/SBUnix/441268cc45d5f133ca9cbe9566d462320303d86e/rootfs/boot/boot0 -------------------------------------------------------------------------------- /rootfs/boot/boot1: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/rajesh5310/SBUnix/441268cc45d5f133ca9cbe9566d462320303d86e/rootfs/boot/boot1 -------------------------------------------------------------------------------- /rootfs/boot/boot2: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/rajesh5310/SBUnix/441268cc45d5f133ca9cbe9566d462320303d86e/rootfs/boot/boot2 -------------------------------------------------------------------------------- /rootfs/boot/cdboot: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/rajesh5310/SBUnix/441268cc45d5f133ca9cbe9566d462320303d86e/rootfs/boot/cdboot -------------------------------------------------------------------------------- /rootfs/boot/defaults/loader.conf: -------------------------------------------------------------------------------- 1 | kernel="kernel" # /boot sub-directory containing kernel and modules 2 | bootfile="kernel" # Kernel name (possibly absolute path) 3 | kernel_options="" # Flags to be passed to the kernel 4 | module_path="/boot/modules" # Set the module search path 5 | 6 | loader_conf_files="/boot/loader.conf" 7 | nextboot_conf="/boot/nextboot.conf" 8 | nextboot_enable="NO" 9 | 10 | verbose_loading="NO" # Set to YES for verbose loader output 11 | 12 | loader_logo="cse506" 13 | autoboot_delay="-1" 14 | -------------------------------------------------------------------------------- /rootfs/boot/gptboot: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/rajesh5310/SBUnix/441268cc45d5f133ca9cbe9566d462320303d86e/rootfs/boot/gptboot -------------------------------------------------------------------------------- /rootfs/boot/kernel/.empty: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/rajesh5310/SBUnix/441268cc45d5f133ca9cbe9566d462320303d86e/rootfs/boot/kernel/.empty -------------------------------------------------------------------------------- /rootfs/boot/kernel/kernel: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/rajesh5310/SBUnix/441268cc45d5f133ca9cbe9566d462320303d86e/rootfs/boot/kernel/kernel -------------------------------------------------------------------------------- /rootfs/boot/loader: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/rajesh5310/SBUnix/441268cc45d5f133ca9cbe9566d462320303d86e/rootfs/boot/loader -------------------------------------------------------------------------------- /rootfs/boot/loader.4th: -------------------------------------------------------------------------------- 1 | \ Copyright (c) 1999 Daniel C. Sobral 2 | \ All rights reserved. 3 | \ 4 | \ Redistribution and use in source and binary forms, with or without 5 | \ modification, are permitted provided that the following conditions 6 | \ are met: 7 | \ 1. Redistributions of source code must retain the above copyright 8 | \ notice, this list of conditions and the following disclaimer. 9 | \ 2. Redistributions in binary form must reproduce the above copyright 10 | \ notice, this list of conditions and the following disclaimer in the 11 | \ documentation and/or other materials provided with the distribution. 12 | \ 13 | \ THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND 14 | \ ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 15 | \ IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 16 | \ ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE 17 | \ FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 18 | \ DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 19 | \ OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 20 | \ HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 21 | \ LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 22 | \ OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 23 | \ SUCH DAMAGE. 24 | \ 25 | \ $FreeBSD: release/9.1.0/sys/boot/forth/loader.4th 230219 2012-01-16 14:55:42Z pluknet $ 26 | 27 | s" arch-i386" environment? [if] [if] 28 | s" loader_version" environment? [if] 29 | 11 < [if] 30 | .( Loader version 1.1+ required) cr 31 | abort 32 | [then] 33 | [else] 34 | .( Could not get loader version!) cr 35 | abort 36 | [then] 37 | [then] [then] 38 | 39 | 256 dictthreshold ! \ 256 cells minimum free space 40 | 2048 dictincrease ! \ 2048 additional cells each time 41 | 42 | include /boot/support.4th 43 | 44 | only forth also support-functions also builtins definitions 45 | 46 | : try-menu-unset 47 | \ menu-unset may not be present 48 | s" beastie_disable" getenv 49 | dup -1 <> if 50 | s" YES" compare-insensitive 0= if 51 | exit 52 | then 53 | else 54 | drop 55 | then 56 | s" menu-unset" 57 | sfind if 58 | execute 59 | else 60 | drop 61 | then 62 | ; 63 | 64 | : boot 65 | 0= if ( interpreted ) get_arguments then 66 | 67 | \ Unload only if a path was passed 68 | dup if 69 | >r over r> swap 70 | c@ [char] - <> if 71 | 0 1 unload drop 72 | else 73 | s" kernelname" getenv? if ( a kernel has been loaded ) 74 | try-menu-unset 75 | 1 boot exit 76 | then 77 | load_kernel_and_modules 78 | ?dup if exit then 79 | try-menu-unset 80 | 0 1 boot exit 81 | then 82 | else 83 | s" kernelname" getenv? if ( a kernel has been loaded ) 84 | try-menu-unset 85 | 1 boot exit 86 | then 87 | load_kernel_and_modules 88 | ?dup if exit then 89 | try-menu-unset 90 | 0 1 boot exit 91 | then 92 | load_kernel_and_modules 93 | ?dup 0= if 0 1 boot then 94 | ; 95 | 96 | \ ***** boot-conf 97 | \ 98 | \ Prepares to boot as specified by loaded configuration files. 99 | 100 | : boot-conf 101 | 0= if ( interpreted ) get_arguments then 102 | 0 1 unload drop 103 | load_kernel_and_modules 104 | ?dup 0= if 0 1 autoboot then 105 | ; 106 | 107 | also forth definitions also builtins 108 | 109 | builtin: boot 110 | builtin: boot-conf 111 | 112 | only forth definitions also support-functions 113 | 114 | include /boot/screen.4th 115 | 116 | \ ***** start 117 | \ 118 | \ Initializes support.4th global variables, sets loader_conf_files, 119 | \ process conf files, and, if any one such file was succesfully 120 | \ read to the end, load kernel and modules. 121 | 122 | : start ( -- ) ( throws: abort & user-defined ) 123 | s" /boot/defaults/loader.conf" initialize 124 | include_conf_files 125 | include_nextboot_file 126 | \ Will *NOT* try to load kernel and modules if no configuration file 127 | \ was succesfully loaded! 128 | any_conf_read? if 129 | load_kernel 130 | load_modules 131 | then 132 | ; 133 | 134 | \ ***** initialize 135 | \ 136 | \ Overrides support.4th initialization word with one that does 137 | \ everything start one does, short of loading the kernel and 138 | \ modules. Returns a flag 139 | 140 | : initialize ( -- flag ) 141 | s" /boot/defaults/loader.conf" initialize 142 | include_conf_files 143 | include_nextboot_file 144 | any_conf_read? 145 | ; 146 | 147 | \ ***** read-conf 148 | \ 149 | \ Read a configuration file, whose name was specified on the command 150 | \ line, if interpreted, or given on the stack, if compiled in. 151 | 152 | : (read-conf) ( addr len -- ) 153 | conf_files string= 154 | include_conf_files \ Will recurse on new loader_conf_files definitions 155 | ; 156 | 157 | : read-conf ( | addr len -- ) ( throws: abort & user-defined ) 158 | state @ if 159 | \ Compiling 160 | postpone (read-conf) 161 | else 162 | \ Interpreting 163 | bl parse (read-conf) 164 | then 165 | ; immediate 166 | 167 | \ show, enable, disable, toggle module loading. They all take module from 168 | \ the next word 169 | 170 | : set-module-flag ( module_addr val -- ) \ set and print flag 171 | over module.flag ! 172 | dup module.name strtype 173 | module.flag @ if ." will be loaded" else ." will not be loaded" then cr 174 | ; 175 | 176 | : enable-module find-module ?dup if true set-module-flag then ; 177 | 178 | : disable-module find-module ?dup if false set-module-flag then ; 179 | 180 | : toggle-module find-module ?dup if dup module.flag @ 0= set-module-flag then ; 181 | 182 | \ ***** show-module 183 | \ 184 | \ Show loading information about a module. 185 | 186 | : show-module ( -- ) find-module ?dup if show-one-module then ; 187 | 188 | \ Words to be used inside configuration files 189 | 190 | : retry false ; \ For use in load error commands 191 | : ignore true ; \ For use in load error commands 192 | 193 | \ Return to strict forth vocabulary 194 | 195 | : #type 196 | over - >r 197 | type 198 | r> spaces 199 | ; 200 | 201 | : .? 2 spaces 2swap 15 #type 2 spaces type cr ; 202 | 203 | : ? 204 | ['] ? execute 205 | s" boot-conf" s" load kernel and modules, then autoboot" .? 206 | s" read-conf" s" read a configuration file" .? 207 | s" enable-module" s" enable loading of a module" .? 208 | s" disable-module" s" disable loading of a module" .? 209 | s" toggle-module" s" toggle loading of a module" .? 210 | s" show-module" s" show module load data" .? 211 | ; 212 | 213 | only forth also 214 | 215 | -------------------------------------------------------------------------------- /rootfs/boot/loader.conf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/rajesh5310/SBUnix/441268cc45d5f133ca9cbe9566d462320303d86e/rootfs/boot/loader.conf -------------------------------------------------------------------------------- /rootfs/boot/loader.rc: -------------------------------------------------------------------------------- 1 | \ Loader.rc 2 | \ $FreeBSD: release/9.1.0/sys/boot/i386/loader/loader.rc 151874 2005-10-30 05:41:42Z scottl $ 3 | \ 4 | \ Includes additional commands 5 | include /boot/loader.4th 6 | 7 | \ Reads and processes loader.conf variables 8 | start 9 | 10 | \ Load in the boot menu 11 | include /boot/beastie.4th 12 | 13 | \ Start the boot menu 14 | \ beastie-start 15 | clear 16 | draw-beastie 17 | -------------------------------------------------------------------------------- /rootfs/boot/mbr: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/rajesh5310/SBUnix/441268cc45d5f133ca9cbe9566d462320303d86e/rootfs/boot/mbr -------------------------------------------------------------------------------- /rootfs/boot/pxeboot: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/rajesh5310/SBUnix/441268cc45d5f133ca9cbe9566d462320303d86e/rootfs/boot/pxeboot -------------------------------------------------------------------------------- /rootfs/boot/screen.4th: -------------------------------------------------------------------------------- 1 | \ Screen manipulation related words. 2 | \ $FreeBSD: release/9.1.0/sys/boot/forth/screen.4th 115410 2003-05-30 09:29:24Z scottl $ 3 | 4 | marker task-screen.4th 5 | 6 | : escc ( -- ) \ emit Esc-[ 7 | 91 27 emit emit 8 | ; 9 | 10 | : ho ( -- ) \ Home cursor 11 | escc 72 emit \ Esc-[H 12 | ; 13 | 14 | : cld ( -- ) \ Clear from current position to end of display 15 | escc 74 emit \ Esc-[J 16 | ; 17 | 18 | : clear ( -- ) \ clear screen 19 | ho cld 20 | ; 21 | 22 | : at-xy ( x y -- ) \ move cursor to x rows, y cols (1-based coords) 23 | escc .# 59 emit .# 72 emit \ Esc-[%d;%dH 24 | ; 25 | 26 | : fg ( x -- ) \ Set foreground color 27 | escc 3 .# .# 109 emit \ Esc-[3%dm 28 | ; 29 | 30 | : bg ( x -- ) \ Set background color 31 | escc 4 .# .# 109 emit \ Esc-[4%dm 32 | ; 33 | 34 | : me ( -- ) \ Mode end (clear attributes) 35 | escc 109 emit 36 | ; 37 | -------------------------------------------------------------------------------- /rootfs/etc/.empty: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/rajesh5310/SBUnix/441268cc45d5f133ca9cbe9566d462320303d86e/rootfs/etc/.empty -------------------------------------------------------------------------------- /rootfs/etc/dummy: -------------------------------------------------------------------------------- 1 | testing 123 2 | -------------------------------------------------------------------------------- /rootfs/lib/.empty: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/rajesh5310/SBUnix/441268cc45d5f133ca9cbe9566d462320303d86e/rootfs/lib/.empty -------------------------------------------------------------------------------- /rootfs/lib/crt1.o: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/rajesh5310/SBUnix/441268cc45d5f133ca9cbe9566d462320303d86e/rootfs/lib/crt1.o -------------------------------------------------------------------------------- /rootfs/lib/ld.so: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/rajesh5310/SBUnix/441268cc45d5f133ca9cbe9566d462320303d86e/rootfs/lib/ld.so -------------------------------------------------------------------------------- /rootfs/lib/libc.a: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/rajesh5310/SBUnix/441268cc45d5f133ca9cbe9566d462320303d86e/rootfs/lib/libc.a -------------------------------------------------------------------------------- /rootfs/lib/libc.so: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/rajesh5310/SBUnix/441268cc45d5f133ca9cbe9566d462320303d86e/rootfs/lib/libc.so -------------------------------------------------------------------------------- /rootfs/shcommands: -------------------------------------------------------------------------------- 1 | #!/bin/sh 2 | echo All supported commands 3 | man 4 | echo 5 | echo List all directories 6 | ls 7 | echo 8 | echo Working processes 9 | ps 10 | echo 11 | echo Display file contents 12 | cat tease 13 | echo Shell sleeping 14 | sleep 2 15 | echo 16 | echo Present Working Directory 17 | pwd 18 | echo 19 | echo cd command 20 | cd bin 21 | echo 22 | echo cd .. Command 23 | cd .. 24 | echo 25 | echo Use vi and cat command to manipulate files in file system hdd/ 26 | cd hdd/ 27 | -------------------------------------------------------------------------------- /rootfs/tease: -------------------------------------------------------------------------------- 1 | #!/bin/sh 2 | ls 3 | cd etc/ 4 | cat dummy 5 | echo working 6 | -------------------------------------------------------------------------------- /rootfs/test: -------------------------------------------------------------------------------- 1 | testing 123 testing 123 testing 123 -------------------------------------------------------------------------------- /superblock: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/rajesh5310/SBUnix/441268cc45d5f133ca9cbe9566d462320303d86e/superblock -------------------------------------------------------------------------------- /sys/assert.h: -------------------------------------------------------------------------------- 1 | 2 | #ifndef ASSERT_H 3 | #define ASSERT_H 4 | 5 | #include 6 | 7 | #define assert(x) \ 8 | do { if (!(x)) print("assertion failed: %s", #x); } while (0) 9 | 10 | // static_assert(x) will generate a compile-time error if 'x' is false. 11 | #define static_assert(x) switch (x) case 0: case (x): 12 | 13 | #endif /* ASSERT_H */ 14 | -------------------------------------------------------------------------------- /sys/gdt.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | /* adapted from Chris Stones, shovelos */ 4 | 5 | #define MAX_GDT 32 6 | 7 | struct sys_segment_descriptor { 8 | uint64_t sd_lolimit:16;/* segment extent (lsb) */ 9 | uint64_t sd_lobase:24; /* segment base address (lsb) */ 10 | uint64_t sd_type:5; /* segment type */ 11 | uint64_t sd_dpl:2; /* segment descriptor priority level */ 12 | uint64_t sd_p:1; /* segment descriptor present */ 13 | uint64_t sd_hilimit:4; /* segment extent (msb) */ 14 | uint64_t sd_xx1:3; /* avl, long and def32 (not used) */ 15 | uint64_t sd_gran:1; /* limit granularity (byte/page) */ 16 | uint64_t sd_hibase:40; /* segment base address (msb) */ 17 | uint64_t sd_xx2:8; /* reserved */ 18 | uint64_t sd_zero:5; /* must be zero */ 19 | uint64_t sd_xx3:19; /* reserved */ 20 | }__attribute__((packed)); 21 | 22 | uint64_t gdt[MAX_GDT] = { 23 | 0, /*** NULL descriptor ***/ 24 | GDT_CS | P | DPL0 | L, /*** kernel code segment descriptor ***/ 25 | GDT_DS | P | W | DPL0, /*** kernel data segment descriptor ***/ 26 | GDT_CS | P | DPL3 | L, /*** user code segment descriptor ***/ 27 | GDT_DS | P | W | DPL3, /*** user data segment descriptor ***/ 28 | 0, 0, /*** TSS ***/ 29 | }; 30 | 31 | struct gdtr_t { 32 | uint16_t size; 33 | uint64_t addr; 34 | }__attribute__((packed)); 35 | 36 | static struct gdtr_t gdtr = { 37 | sizeof(gdt), 38 | (uint64_t)gdt, 39 | }; 40 | 41 | void _x86_64_asm_lgdt(struct gdtr_t* gdtr, uint64_t cs_idx, uint64_t ds_idx); 42 | 43 | void reload_gdt() { 44 | _x86_64_asm_lgdt(&gdtr, 8, 16); 45 | } 46 | 47 | void setup_tss() { 48 | struct sys_segment_descriptor* sd = (struct sys_segment_descriptor*)&gdt[5]; // 6th&7th entry in GDT 49 | sd->sd_lolimit = sizeof(struct tss_t)-1; 50 | sd->sd_lobase = ((uint64_t)&tss); 51 | sd->sd_type = 9; // 386 TSS 52 | sd->sd_dpl = 0; 53 | sd->sd_p = 1; 54 | sd->sd_hilimit = 0; 55 | sd->sd_gran = 0; 56 | sd->sd_hibase = ((uint64_t)&tss) >> 24; 57 | //asm("mov $0x2b,%ax"); 58 | //asm ("ltr %ax"); 59 | 60 | } 61 | -------------------------------------------------------------------------------- /sys/gdt.s: -------------------------------------------------------------------------------- 1 | # 2 | # gdt.s 3 | # 4 | # Created on: Dec 29, 2010 5 | # Author: cds 6 | # 7 | 8 | .text 9 | 10 | ###### 11 | # load a new GDT 12 | # parameter 1: address of gdtr 13 | # parameter 2: new code descriptor offset 14 | # parameter 3: new data descriptor offset 15 | .global _x86_64_asm_lgdt 16 | _x86_64_asm_lgdt: 17 | 18 | lgdt (%rdi) 19 | 20 | pushq %rsi # push code selector 21 | movabsq $.done, %r10 22 | pushq %r10 # push return address 23 | lretq # far-return to new cs descriptor ( the retq below ) 24 | .done: 25 | movq %rdx, %es 26 | movq %rdx, %fs 27 | movq %rdx, %gs 28 | movq %rdx, %ds 29 | movq %rdx, %ss 30 | retq 31 | -------------------------------------------------------------------------------- /sys/idt.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | static void init_idt(); 6 | static void idt_set_gate(int,uint16_t, unsigned , unsigned , unsigned , uint64_t); 7 | 8 | idt_entry_t idt_entries[256]; 9 | idt_ptr_t idt_ptr; 10 | 11 | void reload_idt() 12 | { 13 | init_idt(); 14 | }; 15 | 16 | // Write len copies of val into dest. 17 | 18 | 19 | extern void isr0(); //DUMMY_INTERRUPT(0) 20 | extern void isr1(); 21 | extern void isr2(); 22 | extern void isr3(); 23 | extern void isr4(); 24 | extern void isr5(); 25 | extern void isr6(); 26 | extern void isr7(); 27 | extern void isr8(); 28 | extern void isr9(); 29 | extern void isr10(); 30 | extern void isr11(); 31 | extern void isr12(); 32 | extern void isr13(); 33 | extern void isr14(); 34 | extern void isr15(); 35 | extern void isr16(); 36 | extern void isr17(); 37 | extern void isr18(); 38 | extern void isr19(); 39 | extern void isr20(); 40 | extern void isr21(); 41 | extern void isr22(); 42 | extern void isr23(); 43 | extern void isr24(); 44 | extern void isr25(); 45 | extern void isr26(); 46 | extern void isr27(); 47 | extern void isr28(); 48 | extern void isr29(); 49 | extern void isr30(); 50 | extern void isr31(); 51 | extern void isr32(); 52 | extern void isr33(); 53 | extern void isr128(); 54 | extern void isr43(); 55 | void load_irq() 56 | { 57 | outb(0x20, 0x11); 58 | outb(0xA0, 0x11); 59 | outb(0x21, 0x20); 60 | outb(0xA1, 0x28); 61 | outb(0x21, 0x04); 62 | outb(0xA1, 0x02); 63 | outb(0x21, 0x01); 64 | outb(0xA1, 0x01); 65 | outb(0x21, 0x0); 66 | outb(0xA1, 0x0); 67 | 68 | idt_set_gate(32,8, 0, 0x8E, 0, ((uint64_t)isr32)); 69 | idt_set_gate(33,8, 0, 0x8E, 0, ((uint64_t)isr33)); 70 | idt_set_gate(43,8, 0, 0x8E, 0, ((uint64_t)isr43)); 71 | 72 | // __asm__("sti"); 73 | } 74 | 75 | static void init_idt() 76 | { 77 | memset(&idt_entries, 0, sizeof(idt_entry_t ) * 256); 78 | idt_ptr.limit = sizeof(idt_entry_t) * 256 - 1; 79 | idt_ptr.base = (uint64_t)&idt_entries; 80 | 81 | idt_set_gate(0,8, 0, 0x0e, 0, ((uint64_t)&isr0)); 82 | idt_set_gate(1,8, 0, 0x0e, 0, ((uint64_t)&isr1)); 83 | idt_set_gate(2,8, 0, 0x0e, 0, ((uint64_t)&isr2)); 84 | idt_set_gate(3,8, 0, 0x0e, 0, ((uint64_t)&isr3)); 85 | idt_set_gate(4,8, 0, 0x0e, 0, ((uint64_t)&isr4)); 86 | idt_set_gate(5,8, 0, 0x0e, 0, ((uint64_t)&isr5)); 87 | idt_set_gate(6,8, 0, 0x0e, 0, ((uint64_t)&isr6)); 88 | idt_set_gate(7,8, 0, 0x0e, 0, ((uint64_t)&isr7)); 89 | idt_set_gate(8,8, 0, 0x0e, 0, ((uint64_t)&isr8)); 90 | idt_set_gate(9,8, 0, 0x0e, 0, ((uint64_t)&isr9)); 91 | idt_set_gate(10,8, 0, 0x0e, 0, ((uint64_t)&isr10)); 92 | idt_set_gate(11,8, 0, 0x0e, 0, ((uint64_t)&isr11)); 93 | idt_set_gate(12,8, 0, 0x0e, 0, ((uint64_t)&isr12)); 94 | idt_set_gate(13,8, 0, 0x0e, 0, ((uint64_t)&isr13)); 95 | idt_set_gate(14,8, 0, 0x0e, 0, ((uint64_t)&isr14)); 96 | idt_set_gate(15,8, 0, 0x0e, 0, ((uint64_t)&isr15)); 97 | idt_set_gate(16,8, 0, 0x0e, 0, ((uint64_t)&isr16)); 98 | idt_set_gate(17,8, 0, 0x0e, 0, ((uint64_t)&isr17)); 99 | idt_set_gate(18,8, 0, 0x0e, 0, ((uint64_t)&isr18)); 100 | idt_set_gate(19,8, 0, 0x0e, 0, ((uint64_t)&isr19)); 101 | idt_set_gate(20,8, 0, 0x0e, 0, ((uint64_t)&isr20)); 102 | idt_set_gate(21,8, 0, 0x0e, 0, ((uint64_t)&isr21)); 103 | idt_set_gate(22,8, 0, 0x0e, 0, ((uint64_t)&isr22)); 104 | idt_set_gate(23,8, 0, 0x0e, 0, ((uint64_t)&isr23)); 105 | idt_set_gate(24,8, 0, 0x0e, 0, ((uint64_t)&isr24)); 106 | idt_set_gate(25,8, 0, 0x0e, 0, ((uint64_t)&isr25)); 107 | idt_set_gate(26,8, 0, 0x0e, 0, ((uint64_t)&isr26)); 108 | idt_set_gate(27,8, 0, 0x0e, 0, ((uint64_t)&isr27)); 109 | idt_set_gate(28,8, 0, 0x0e, 0, ((uint64_t)&isr28)); 110 | idt_set_gate(29,8, 0, 0x0e, 0, ((uint64_t)&isr29)); 111 | idt_set_gate(30,8, 0, 0x0e, 0, ((uint64_t)&isr30)); 112 | idt_set_gate(31,8, 0, 0x0e, 0, ((uint64_t)&isr31)); 113 | idt_set_gate(128,8, 0, 0x0e, 3, ((uint64_t)&isr128)); 114 | _x86_64_asm_igdt(&idt_ptr); 115 | }; 116 | 117 | static void idt_set_gate(int num,uint16_t selector, unsigned ist, unsigned type, unsigned dpl, uint64_t offset) 118 | { 119 | idt_entries[num].offset_low = (offset) & 0xFFFF; 120 | idt_entries[num].selector = selector; 121 | idt_entries[num].ist = ist; 122 | idt_entries[num].reserved0 = 0; 123 | idt_entries[num].type = type; 124 | idt_entries[num].zero = 0; 125 | idt_entries[num].dpl = dpl; 126 | idt_entries[num].p = 1; 127 | idt_entries[num].offset_mid = (offset >> 16) & 0xFFFF; 128 | idt_entries[num].offset_high = (offset >> 32) & 0xFFFFFFFF; 129 | }; 130 | -------------------------------------------------------------------------------- /sys/idt.s: -------------------------------------------------------------------------------- 1 | # Author: cds 2 | # 3 | 4 | .text 5 | 6 | ###### 7 | # load a new IDT 8 | # parameter 1: address of idtr 9 | .global _x86_64_asm_igdt 10 | _x86_64_asm_igdt: 11 | lidt (%rdi) 12 | retq # far-return to new cs descriptor ( the retq below ) 13 | 14 | -------------------------------------------------------------------------------- /sys/isr.s: -------------------------------------------------------------------------------- 1 | #author: cds 2 | # 3 | 4 | .text 5 | 6 | ###### 7 | # load a new IDT 8 | # parameter 1: address of gdtr 9 | # parameter 2: new code descriptor offset 10 | # parameter 3: new data descriptor offset 11 | 12 | .macro set_interrupt index 13 | .global isr\index 14 | isr\index: 15 | cli 16 | pushq %rax 17 | pushq %rbx 18 | pushq %rcx 19 | pushq %rdx 20 | pushq %rsi 21 | pushq %rdi 22 | pushq %rbp 23 | pushq %r8 24 | pushq %r9 25 | pushq %r10 26 | pushq %r11 27 | call isr_handler\index 28 | popq %r11 29 | popq %r10 30 | popq %r9 31 | popq %r8 32 | popq %rbp 33 | popq %rdi 34 | popq %rsi 35 | popq %rdx 36 | popq %rcx 37 | popq %rbx 38 | popq %rax 39 | sti 40 | iretq 41 | .endm 42 | 43 | .global isr128 44 | isr128: 45 | # cli 46 | pushq %rbx 47 | pushq %rcx 48 | pushq %rdx 49 | pushq %rsi 50 | pushq %rdi 51 | pushq %rbp 52 | pushq %r8 53 | pushq %r9 54 | pushq %r10 55 | pushq %r11 56 | pushq %r12 57 | pushq %r13 58 | pushq %r14 59 | pushq %r15 60 | call isr_handler128 61 | popq %r15 62 | popq %r14 63 | popq %r13 64 | popq %r12 65 | popq %r11 66 | popq %r10 67 | popq %r9 68 | popq %r8 69 | popq %rbp 70 | popq %rdi 71 | popq %rsi 72 | popq %rdx 73 | popq %rcx 74 | popq %rbx 75 | # sti 76 | iretq 77 | .endm 78 | 79 | .global isr32 80 | isr32: 81 | cli 82 | pushq %rax 83 | pushq %rbx 84 | pushq %rcx 85 | pushq %rdx 86 | pushq %rsi 87 | pushq %rdi 88 | pushq %rbp 89 | pushq %r8 90 | pushq %r9 91 | pushq %r10 92 | pushq %r11 93 | pushq %r12 94 | pushq %r13 95 | pushq %r14 96 | pushq %r15 97 | # movq %rsp,%rdi 98 | # addq $72,%rdi 99 | call timer_callback 100 | call schedule 101 | popq %r15 102 | popq %r14 103 | popq %r13 104 | popq %r12 105 | popq %r11 106 | popq %r10 107 | popq %r9 108 | popq %r8 109 | popq %rbp 110 | popq %rdi 111 | popq %rsi 112 | popq %rdx 113 | popq %rcx 114 | popq %rbx 115 | popq %rax 116 | sti 117 | iretq 118 | .endm 119 | 120 | set_interrupt 0 121 | set_interrupt 1 122 | set_interrupt 2 123 | set_interrupt 3 124 | set_interrupt 4 125 | set_interrupt 5 126 | set_interrupt 6 127 | set_interrupt 7 128 | set_interrupt 8 129 | set_interrupt 9 130 | set_interrupt 10 131 | set_interrupt 11 132 | set_interrupt 12 133 | set_interrupt 13 134 | set_interrupt 14 135 | set_interrupt 15 136 | set_interrupt 16 137 | set_interrupt 17 138 | set_interrupt 18 139 | set_interrupt 19 140 | set_interrupt 20 141 | set_interrupt 21 142 | set_interrupt 22 143 | set_interrupt 23 144 | set_interrupt 24 145 | set_interrupt 25 146 | set_interrupt 26 147 | set_interrupt 27 148 | set_interrupt 28 149 | set_interrupt 29 150 | set_interrupt 30 151 | set_interrupt 31 152 | #set_interrupt 32 153 | set_interrupt 33 154 | set_interrupt 43 155 | -------------------------------------------------------------------------------- /sys/kbc.c: -------------------------------------------------------------------------------- 1 | /* 2 | * kbc.c 3 | * 4 | * Created on: 9 Aug 2011 5 | * Author: Chris Stones 6 | */ 7 | 8 | #include 9 | #include 10 | #include 11 | #include 12 | #include 13 | #include 14 | 15 | char keyboard_buf[100]; 16 | volatile int is_taking_input = 0; 17 | int keyboard_buf_count = 0; 18 | uint64_t video_ptr_kb; 19 | extern volatile char * video_memory; 20 | void reset_keyboard_buf() 21 | { 22 | int i = 0; 23 | for(i = 0; i < 100; i++) 24 | keyboard_buf[i] = '\0'; 25 | } 26 | 27 | /* 28 | * Routine that handles keyboard interrupt 29 | */ 30 | void kbc_irq() { 31 | 32 | static uint8_t shift = 0; 33 | static uint8_t ctrl= 0; 34 | 35 | // Read scan from port 0x60 36 | uint8_t b = port_inb(0x60); 37 | 38 | //Detect if shift is pressed 39 | switch(b) 40 | { 41 | case KBCSC_LEFTSHIFT: 42 | shift |= 1; 43 | break; 44 | case KBCSC_RIGHTSHIFT: 45 | shift |= 2; 46 | break; 47 | case (KBCSC_LEFTSHIFT | 0x80): 48 | shift &= ~1; 49 | break; 50 | case (KBCSC_RIGHTSHIFT | 0x80): 51 | shift &= ~2; 52 | /*case (KBCSC_LEFTCTRL | 0x80): 53 | ctrl |= 1; 54 | break; 55 | case (KBCSC_LEFTCTRL): 56 | ctrl |= 1; 57 | break;*/ 58 | /*case (KBCSC_RIGHTCTRL | 0x80): 59 | shift &= ~2;*/ 60 | 61 | default: 62 | break; 63 | } 64 | 65 | //Call method that converts scan code to character 66 | uint8_t c = get_tochar(b, shift, ctrl); 67 | if(c!=0) 68 | { 69 | print("%c", c); 70 | print_line(1,24, "%c", c); 71 | } 72 | 73 | } 74 | 75 | /* 76 | * Method that prints character as per the shift value 77 | */ 78 | static uint8_t char_shift_adjust(uint8_t c, uint8_t shift) { 79 | 80 | return c + (shift ? ('A'-'a') : 0); 81 | } 82 | 83 | /* 84 | * Method that converts scan code to character 85 | */ 86 | uint8_t get_tochar(uint8_t b, uint8_t shift , uint8_t ctrl) { 87 | 88 | char ch = '\0'; 89 | 90 | if(b==KBCSC_SPACE) 91 | ch = ' '; 92 | 93 | if(b==KBCSC_RETURN) 94 | { 95 | keyboard_buf[keyboard_buf_count++] = '\0'; 96 | keyboard_buf_count = 0; 97 | is_taking_input = 0; 98 | //print("asd\n"); 99 | } 100 | if(b==KBCSC_BACKSPACE) 101 | { 102 | if((uint64_t)video_memory > video_ptr_kb) 103 | { 104 | keyboard_buf[keyboard_buf_count] = '\0'; 105 | keyboard_buf_count--; 106 | print_backspace(); 107 | } 108 | } 109 | if((b >= KBCSC_Q) && (b <= KBCSC_P)) 110 | { 111 | ch = char_shift_adjust("qwertyuiop"[b-KBCSC_Q], shift); 112 | } 113 | 114 | 115 | if((b >= KBCSC_A) && (b <= KBCSC_L)) 116 | ch = char_shift_adjust("asdfghjkl"[b-KBCSC_A], shift); 117 | 118 | if((b >= KBCSC_Z) && (b <= KBCSC_M)) 119 | ch = char_shift_adjust("zxcvbnm"[b-KBCSC_Z], shift); 120 | 121 | if((b >= KBCSC_1) && (b <= KBCSC_9)) { 122 | if(shift) 123 | ch = "!@#$%^&*("[b-KBCSC_1]; 124 | ch = '1' + b-KBCSC_1; 125 | } 126 | 127 | if((b >= KBCSC_OPEN_CURL) && (b <= KBCSC_CLOSE_CURL)) { 128 | if(shift) 129 | ch = "{}"[b-KBCSC_OPEN_CURL]; 130 | ch = "[]"[b-KBCSC_OPEN_CURL]; 131 | } 132 | 133 | if((b >= KBCSC_COLON) && (b <= KBCSC_TILDA)) { 134 | if(shift) 135 | ch = ":\"~"[b-KBCSC_COLON]; 136 | ch = ";'`"[b-KBCSC_COLON]; 137 | } 138 | 139 | if((b >= KBCSC_COMMA) && (b <= KBCSC_SLASH)) { 140 | if(shift) 141 | ch = "<>?"[b-KBCSC_COMMA]; 142 | ch = ",./"[b-KBCSC_COMMA]; 143 | } 144 | 145 | 146 | if((b >= KBCSC_UNDERSCORE_MINUS) && (b <= KBCSC_EQUALS_PLUS)) { 147 | if(shift) 148 | ch = "_+"[b-KBCSC_UNDERSCORE_MINUS]; 149 | ch = "-="[b-KBCSC_UNDERSCORE_MINUS]; 150 | } 151 | 152 | 153 | if(b == KBCSC_OR) 154 | ch = shift ? '|' : '\\'; 155 | 156 | 157 | if(b == KBCSC_0) 158 | ch = shift ? ')' : '0'; 159 | 160 | if(is_taking_input == 1 && ch != '\0') 161 | { 162 | keyboard_buf[keyboard_buf_count++] = ch; 163 | //print_line(10, 10 ,"%d %d", keyboard_buf_count, ch); 164 | } 165 | return ch; 166 | } 167 | 168 | void scanf(char * buf) 169 | { 170 | video_ptr_kb = (uint64_t)video_memory; 171 | is_taking_input = 1; 172 | while(is_taking_input == 1); 173 | // print("done"); 174 | strcpy(buf, keyboard_buf); 175 | reset_keyboard_buf(); 176 | } 177 | -------------------------------------------------------------------------------- /sys/keyboard.c: -------------------------------------------------------------------------------- 1 | /* @name : keyboard.c 2 | * @author : rgolani, abmishra, skandalamsri 3 | * @last updated date : 25th September, 2013 4 | */ 5 | 6 | #include 7 | #include 8 | #include 9 | #include 10 | 11 | /* 12 | * Routine that handles keyboard interrupt 13 | */ 14 | void handle_keyboard_irq() { 15 | 16 | static uint8_t shift = 0; 17 | static uint8_t ctrl= 0; 18 | 19 | // Read scan from port 0x60 20 | uint8_t b = port_inb(0x60); 21 | 22 | //Detect if shift is pressed 23 | switch(b) 24 | { 25 | case KBCSC_LEFTSHIFT: 26 | shift |= 1; 27 | break; 28 | case KBCSC_RIGHTSHIFT: 29 | shift |= 2; 30 | break; 31 | case (KBCSC_LEFTSHIFT | 0x80): 32 | shift &= ~1; 33 | break; 34 | case (KBCSC_RIGHTSHIFT | 0x80): 35 | shift &= ~2; 36 | /*case (KBCSC_LEFTCTRL | 0x80): 37 | ctrl |= 1; 38 | break; 39 | case (KBCSC_LEFTCTRL): 40 | ctrl |= 1; 41 | break;*/ 42 | /*case (KBCSC_RIGHTCTRL | 0x80): 43 | shift &= ~2;*/ 44 | 45 | default: 46 | break; 47 | } 48 | 49 | //Call method that converts scan code to character 50 | uint8_t c = get_tochar(b, shift, ctrl); 51 | if(c!=0) 52 | { 53 | print("%c", c); 54 | print_line(1,23, "%c", c); 55 | } 56 | 57 | } 58 | 59 | /* 60 | * Method that prints character as per the shift value 61 | */ 62 | static uint8_t char_shift_adjust(uint8_t c, uint8_t shift) { 63 | 64 | return c + (shift ? ('A'-'a') : 0); 65 | } 66 | 67 | /* 68 | * Method that converts scan code to character 69 | */ 70 | uint8_t get_tochar(uint8_t b, uint8_t shift , uint8_t ctrl) { 71 | 72 | 73 | if(b==KBCSC_SPACE) 74 | return ' '; 75 | 76 | if(b==KBCSC_RETURN) 77 | print("\n"); 78 | 79 | if(b==KBCSC_BACKSPACE) 80 | print_backspace(); 81 | 82 | if((b >= KBCSC_Q) && (b <= KBCSC_P)) 83 | { 84 | return char_shift_adjust("qwertyuiop"[b-KBCSC_Q], shift); 85 | } 86 | 87 | 88 | if((b >= KBCSC_A) && (b <= KBCSC_L)) 89 | return char_shift_adjust("asdfghjkl"[b-KBCSC_A], shift); 90 | 91 | if((b >= KBCSC_Z) && (b <= KBCSC_M)) 92 | return char_shift_adjust("zxcvbnm"[b-KBCSC_Z], shift); 93 | 94 | if((b >= KBCSC_1) && (b <= KBCSC_9)) { 95 | if(shift) 96 | return "!@#$%^&*("[b-KBCSC_1]; 97 | return '1' + b-KBCSC_1; 98 | } 99 | 100 | if((b >= KBCSC_OPEN_CURL) && (b <= KBCSC_CLOSE_CURL)) { 101 | if(shift) 102 | return "{}"[b-KBCSC_OPEN_CURL]; 103 | return "[]"[b-KBCSC_OPEN_CURL]; 104 | } 105 | 106 | if((b >= KBCSC_COLON) && (b <= KBCSC_TILDE)) { 107 | if(shift) 108 | return ":\"~"[b-KBCSC_COLON]; 109 | return ";'`"[b-KBCSC_COLON]; 110 | } 111 | 112 | if((b >= KBCSC_COMMA) && (b <= KBCSC_SLASH)) { 113 | if(shift) 114 | return "<>?"[b-KBCSC_COMMA]; 115 | return ",./"[b-KBCSC_COMMA]; 116 | } 117 | 118 | 119 | if((b >= KBCSC_UNDERSCORE_MINUS) && (b <= KBCSC_EQUALS_PLUS)) { 120 | if(shift) 121 | return "_+"[b-KBCSC_UNDERSCORE_MINUS]; 122 | return "-="[b-KBCSC_UNDERSCORE_MINUS]; 123 | } 124 | 125 | 126 | if(b == KBCSC_OR) 127 | return shift ? '|' : '\\'; 128 | 129 | 130 | if(b == KBCSC_0) 131 | return shift ? ')' : '0'; 132 | 133 | return 0; 134 | } 135 | 136 | -------------------------------------------------------------------------------- /sys/main.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | #include 6 | #include 7 | #include 8 | #include 9 | #include 10 | #include 11 | #include 12 | #include 13 | 14 | #define INITIAL_STACK_SIZE 4096 15 | char stack[INITIAL_STACK_SIZE]; 16 | uint32_t* loader_stack; 17 | extern char kernmem, physbase; 18 | 19 | extern pml4e_t *boot_pml4e; 20 | extern physaddr_t boot_cr3; 21 | // struct task_struct pcb2; 22 | uint64_t *pages_for_ahci_start; 23 | uint64_t *pages_for_ahci_start_virtual; 24 | 25 | uint64_t bar5; 26 | void start(uint32_t* modulep, void* physbase, void* physfree) 27 | { 28 | //Memory Init 29 | mm_init(modulep,physbase,physfree); 30 | 31 | //Memory for PCI 32 | pages_for_ahci_start_virtual = (uint64_t *)kmalloc_ahci(32*4096); 33 | pages_for_ahci_start = (uint64_t *)PADDR(pages_for_ahci_start_virtual); 34 | pages_for_ahci_start = (uint64_t *)(((uint64_t)pages_for_ahci_start+0x0FFF) & ~ 0x0FFF); 35 | pages_for_ahci_start_virtual = (uint64_t *)(((uint64_t)pages_for_ahci_start_virtual+0x0FFF) & ~ 0x0FFF); 36 | // print("\n vir %x %x", pages_for_ahci_start_virtual, pages_for_ahci_start_virtual + 32*4096); 37 | // print("\n phy %x %x", pages_for_ahci_start, pages_for_ahci_start + 32*4096); 38 | 39 | //Initial setup 40 | reload_gdt(); 41 | reload_idt(); 42 | load_irq(); 43 | setup_tss(); 44 | print_line(30, 0, "Welcome To SBUnix"); 45 | 46 | //Kernel process that will be at 0th place in ready queue 47 | struct task_struct *pcb0 = (struct task_struct *)kmalloc(sizeof(struct task_struct)); //kernel 48 | pcb0->pml4e =(uint64_t)boot_pml4e; // kernel's page table 49 | pcb0->cr3 = boot_cr3; // kernel's page table 50 | pcb0->pid = 0; // I'm kernel init process so pid 0 51 | pcb0->stack =(uint64_t *)stack; //my stack is already created by prof :) 52 | process_count = 0; //at this point we don't have any other process in ready_queue so go ahead and create one and update this 53 | sleeping_process_count = 0; // at this point no body is sleeping 54 | // initialize processes queue 55 | int i=0; 56 | for(i=0;i<100;i++) { 57 | zombie_queue[i] = 0; // no process is zombie 58 | } 59 | 60 | ready_queue[0] =(uint64_t )pcb0; //kernel 61 | 62 | //User processes for test 63 | char fname1[]="bin/sh"; //Shell program 64 | // char fname2[]="bin/malloc"; //Scanf and malloc 65 | // char fname3[]="bin/fork"; // Fork test 66 | // char fname4[]="bin/fs"; // Fork test 67 | // char fname5[]="bin/execvpe"; // Execvpe test 68 | 69 | struct task_struct *pcb1 = malloc_pcb(fname1); // If you remove this shell wont start 70 | // struct task_struct *pcb2 = malloc_pcb(fname5); 71 | // struct task_struct *pcb3 = malloc_pcb(fname2); 72 | // struct task_struct *pcb4 = malloc_pcb(fname3); 73 | //struct task_struct *pcb5 = malloc_pcb(fname4); 74 | 75 | //print("pcb1 %x pcb2 %x rsp1 %x rsp2 %x\n",pcb1,pcb2,pcb1->rsp,pcb2->rsp); 76 | print("pointer %p",pcb1); 77 | //print("pointer %p",pcb2); 78 | //print("pointer %p",pcb3); 79 | //print("pointer %p",pcb4); 80 | //print("pointer %p",pcb5); 81 | 82 | //Initialize tarfs and Hard disk file system 83 | tarfs_init(); 84 | probe_port((HBA_MEM *)(0xFFFFFFFF00000000+(uintptr_t)bar5)); 85 | inialize_sata_table(); 86 | 87 | asm volatile("mov $0x2b,%ax"); 88 | asm volatile("ltr %ax"); 89 | init_timer(); 90 | __asm__("sti"); 91 | while(1); 92 | } 93 | 94 | void boot(void) 95 | { 96 | // note: function changes rsp, local stack variables can't be practically used 97 | register char *temp1, *temp2; 98 | __asm__( 99 | "movq %%rsp, %0;" 100 | "movq %1, %%rsp;" 101 | :"=g"(loader_stack) 102 | :"r"(&stack[INITIAL_STACK_SIZE]) 103 | ); 104 | start( 105 | (uint32_t*)((char*)(uint64_t)loader_stack[3] + (uint64_t)&kernmem - (uint64_t)&physbase), 106 | &physbase, 107 | (void*)(uint64_t)loader_stack[4] 108 | ); 109 | for( 110 | temp1 = "!!!!! start() returned !!!!!", temp2 = (char*)0xb8000; 111 | *temp1; 112 | temp1 += 1, temp2 += 2 113 | ) *temp2 = *temp1; 114 | while(1); 115 | } 116 | -------------------------------------------------------------------------------- /sys/ports.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | inline void outb(uint16_t port, uint8_t data) { 4 | 5 | __asm__ __volatile( "outb %0, %1;" 6 | : /* void */ 7 | : "a" (data), "d" (port)); 8 | 9 | } 10 | 11 | void memset(void *dest, uint8_t val, uint32_t len) 12 | { 13 | uint8_t *temp = (uint8_t *)dest; 14 | for ( ; len != 0; len--) *temp++ = val; 15 | } 16 | 17 | inline uint8_t port_inb(uint16_t port) { 18 | 19 | uint8_t ret; 20 | __asm__ __volatile__( "inb %1, %0;" 21 | :"=a" (ret) 22 | :"d" (port) ); 23 | return ret; 24 | } 25 | -------------------------------------------------------------------------------- /sys/process.s: -------------------------------------------------------------------------------- 1 | .text 2 | .global pushAllReg 3 | pushAllReg: 4 | pushq %rax 5 | pushq %rcx 6 | pushq %rdx 7 | pushq %rsi 8 | pushq %rdi 9 | pushq %r8 10 | pushq %r9 11 | pushq %r10 12 | pushq %r11 13 | 14 | .global popAllReg 15 | popAllReg: 16 | popq %r11 17 | popq %r10 18 | popq %r9 19 | popq %r8 20 | popq %rdi 21 | popq %rsi 22 | popq %rdx 23 | popq %rcx 24 | popq %rax 25 | -------------------------------------------------------------------------------- /sys/string.c: -------------------------------------------------------------------------------- 1 | // Basic string routines. Not hardware optimized, but not shabby. 2 | 3 | #include 4 | 5 | // Using assembly for memset/memmove 6 | // makes some difference on real hardware, 7 | // but it makes an even bigger difference on bochs. 8 | // Primespipe runs 3x faster this way. 9 | #define ASM 1 10 | 11 | int strlen(const char *s) { 12 | int n; 13 | 14 | for (n = 0; *s != '\0'; s++) 15 | n++; 16 | return n; 17 | } 18 | 19 | int strnlen(const char *s, size_t size) { 20 | int n; 21 | 22 | for (n = 0; size > 0 && *s != '\0'; s++, size--) 23 | n++; 24 | return n; 25 | } 26 | 27 | char * 28 | strcpy(char *dst, const char *src) { 29 | char *ret; 30 | 31 | ret = dst; 32 | while ((*dst++ = *src++) != '\0') 33 | /* do nothing */; 34 | return ret; 35 | } 36 | 37 | char * 38 | strcat(char *dst, const char *src) { 39 | int len = strlen(dst); 40 | strcpy(dst + len, src); 41 | return dst; 42 | } 43 | 44 | char * 45 | strncpy(char *dst, const char *src, size_t size) { 46 | size_t i; 47 | char *ret; 48 | 49 | ret = dst; 50 | for (i = 0; i < size; i++) { 51 | *dst++ = *src; 52 | // If strlen(src) < size, null-pad 'dst' out to 'size' chars 53 | if (*src != '\0') 54 | src++; 55 | } 56 | return ret; 57 | } 58 | 59 | size_t strlcpy(char *dst, const char *src, size_t size) { 60 | char *dst_in; 61 | 62 | dst_in = dst; 63 | if (size > 0) { 64 | while (--size > 0 && *src != '\0') 65 | *dst++ = *src++; 66 | *dst = '\0'; 67 | } 68 | return dst - dst_in; 69 | } 70 | 71 | int strcmp(const char *p, const char *q) { 72 | while (*p || *q) { 73 | if (*p != *q) 74 | return -1; 75 | p++, q++; 76 | } 77 | return 0; 78 | } 79 | 80 | int strncmp(const char *p, const char *q, size_t n) { 81 | while (n > 0 && *p && *p == *q) 82 | n--, p++, q++; 83 | if (n == 0) 84 | return 0; 85 | else 86 | return (int) ((unsigned char) *p - (unsigned char) *q); 87 | } 88 | 89 | // Return a pointer to the first occurrence of 'c' in 's', 90 | // or a null pointer if the string has no 'c'. 91 | char * 92 | strchr(const char *s, char c) { 93 | for (; *s; s++) 94 | if (*s == c) 95 | return (char *) s; 96 | return 0; 97 | } 98 | 99 | // Return a pointer to the first occurrence of 'c' in 's', 100 | // or a pointer to the string-ending null character if the string has no 'c'. 101 | char * 102 | strfind(const char *s, char c) { 103 | for (; *s; s++) 104 | if (*s == c) 105 | break; 106 | return (char *) s; 107 | } 108 | 109 | int memcmp(const void *v1, const void *v2, size_t n) { 110 | const uint8_t *s1 = (const uint8_t *) v1; 111 | const uint8_t *s2 = (const uint8_t *) v2; 112 | 113 | while (n-- > 0) { 114 | if (*s1 != *s2) 115 | return (int) *s1 - (int) *s2; 116 | s1++, s2++; 117 | } 118 | 119 | return 0; 120 | } 121 | 122 | void* memmove(void *dst, const void *src, size_t n) { 123 | const char *s; 124 | char *d; 125 | 126 | s = src; 127 | d = dst; 128 | if (s < d && s + n > d) { 129 | s += n; 130 | d += n; 131 | while (n-- > 0) 132 | *--d = *--s; 133 | } else 134 | while (n-- > 0) 135 | *d++ = *s++; 136 | 137 | return dst; 138 | } 139 | 140 | void * 141 | memcpy(void *dst, const void *src, size_t n) { 142 | return memmove(dst, src, n); 143 | } 144 | 145 | void * 146 | memfind(const void *s, int c, size_t n) { 147 | const void *ends = (const char *) s + n; 148 | for (; s < ends; s++) 149 | if (*(const unsigned char *) s == (unsigned char) c) 150 | break; 151 | return (void *) s; 152 | } 153 | 154 | long strtol(const char *s, char **endptr, int base) { 155 | int neg = 0; 156 | long val = 0; 157 | 158 | // gobble initial whitespace 159 | while (*s == ' ' || *s == '\t') 160 | s++; 161 | 162 | // plus/minus sign 163 | if (*s == '+') 164 | s++; 165 | else if (*s == '-') 166 | s++, neg = 1; 167 | 168 | // hex or octal base prefix 169 | if ((base == 0 || base == 16) && (s[0] == '0' && s[1] == 'x')) 170 | s += 2, base = 16; 171 | else if (base == 0 && s[0] == '0') 172 | s++, base = 8; 173 | else if (base == 0) 174 | base = 10; 175 | 176 | // digits 177 | while (1) { 178 | int dig; 179 | 180 | if (*s >= '0' && *s <= '9') 181 | dig = *s - '0'; 182 | else if (*s >= 'a' && *s <= 'z') 183 | dig = *s - 'a' + 10; 184 | else if (*s >= 'A' && *s <= 'Z') 185 | dig = *s - 'A' + 10; 186 | else 187 | break; 188 | if (dig >= base) 189 | break; 190 | s++, val = (val * base) + dig; 191 | // we don't properly detect overflow! 192 | } 193 | 194 | if (endptr) 195 | *endptr = (char *) s; 196 | return (neg ? -val : val); 197 | } 198 | char *trimwhitespace(char *str) 199 | { 200 | char *end; 201 | 202 | // Trim leading space 203 | while(*str == ' ') str++; 204 | 205 | if(*str == 0) // All spaces? 206 | return str; 207 | 208 | // Trim trailing space 209 | end = str + strlen(str) - 1; 210 | while(end > str && *end == ' ') end--; 211 | 212 | // Write new null terminator 213 | *(end+1) = 0; 214 | 215 | return str; 216 | } 217 | -------------------------------------------------------------------------------- /sys/timer.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | #include 6 | #include 7 | #include 8 | #include 9 | 10 | uint32_t tick = 0; 11 | volatile int sleep_time = 0; 12 | void timer_callback() 13 | { 14 | tick++; 15 | 16 | outb(0x20, 0x20); 17 | if((tick % 18) == 0) 18 | { 19 | uint32_t time_secs, time_mins=0, time_hours=0; 20 | time_secs = tick/100; 21 | 22 | time_hours = time_secs/3600; 23 | 24 | if(time_secs >= 3600) 25 | time_mins = time_secs%3600; 26 | else if(time_secs < 3600 && time_secs >= 60) 27 | time_mins = time_secs/60; 28 | else 29 | time_mins = 0; 30 | 31 | if(time_secs==60) { 32 | time_secs=0; 33 | } 34 | else if(time_secs > 60) { 35 | time_secs=time_secs%60; 36 | } 37 | 38 | print_line(40, 24, "%s" "%d : %d : %d", "Time since boot (hh:mm:ss): ", time_hours, time_mins, time_secs); 39 | 40 | } 41 | if(sleep_time != 0) 42 | { 43 | sleep_time--; 44 | } 45 | 46 | 47 | } 48 | 49 | void init_timer() 50 | { 51 | // Firstly, register our timer callback. 52 | //irq_install_handler(0, &timer_callback); 53 | 54 | // The value we send to the PIT is the value to divide it's input clock 55 | // (1193180 Hz) by, to get our required frequency. Important to note is 56 | // that the divisor must be small enough to fit into 16-bits. 57 | 58 | int frequency = 100; 59 | uint16_t divisor = 1193180/frequency; 60 | // Send the command byte. 61 | outb(0x43, 0x36); 62 | 63 | // Divisor has to be sent byte-wise, so split here into upper/lower bytes. 64 | uint8_t l = (uint8_t)(divisor & 0xFF); 65 | uint8_t h = (uint8_t)(divisor>>8); 66 | 67 | // Send the frequency divisor. 68 | outb(0x40, l); 69 | outb(0x40, h); 70 | } 71 | 72 | void sleep_shell(int secs) 73 | { 74 | sleep_time = secs*100; 75 | while(sleep_time != 0); 76 | 77 | //print("\n%d sec passed", secs); 78 | } 79 | -------------------------------------------------------------------------------- /sys/utils.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | #include 6 | ///#include 7 | long stoi(const char *s) 8 | { 9 | long i; 10 | i = 0; 11 | while(*s >= '0' && *s <= '9') 12 | { 13 | i = i * 10 + (*s - '0'); 14 | s++; 15 | } 16 | return i; 17 | } 18 | uint64_t power (uint64_t x, int e) { 19 | if (e == 0) return 1; 20 | 21 | return x * power(x, e-1); 22 | } 23 | 24 | uint64_t octalToDecimal(uint64_t octal) 25 | { 26 | uint64_t decimal = 0, i=0; 27 | while(octal!=0){ 28 | decimal = decimal + (octal % 10) * power(8,i++); 29 | octal = octal/10; 30 | } 31 | return decimal; 32 | } 33 | 34 | -------------------------------------------------------------------------------- /sys/vmem.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | //FFFFFFFF80000000 5 | void setup_page_tables(char* physfree); 6 | char phy_mem_map[NUMBER_OF_PAGES/8]; //1 bit for each 8 bits ; 1 character takes 8 bits and can hence store 8 pages.24b000_____ 7 | uint64_t kmalloc(uint64_t sz, char *physfree, int align); 8 | void switch_page_directory(page_dir *dir); 9 | extern void load_cr3(page_dir *dir); 10 | //Intialization of virtual memory 11 | void init_phy_mem(uint32_t* modulep, void* physbase, char* physfree) 12 | { 13 | uint64_t total_phy_mem = 0, avail_phy_mem = 0, length=0; 14 | uint64_t i = 0; 15 | 16 | //List available memory locations 17 | while(modulep[0] != 0x9001) modulep += modulep[1]+2; 18 | 19 | for(smap = (struct smap_t*)(modulep+2); smap < (struct smap_t*)((char*)modulep+modulep[1]+2*4); ++smap) 20 | { 21 | if (smap->type == 1 /* memory */ && smap->length != 0) 22 | { 23 | print("\nAvailable Physical Memory [%x-%x]", smap->base, smap->base + smap->length); 24 | avail_phy_mem += smap->length; 25 | length += smap->length; 26 | } 27 | else 28 | print("\nRESERVED mem [%x-%x] SIZE: %u ", smap->base, smap->base + smap->length, smap->length); 29 | total_phy_mem += smap->length; 30 | } 31 | 32 | print("\nModulep [0] = %p", modulep[0]); 33 | 34 | memset(phy_mem_map, 0x00, sizeof(phy_mem_map)); //Set All bits Available 35 | 36 | // SET BITS UNAVAILABLE FOR the pages corresponding from physbaffffffff80205018se to physfree 37 | for (i = (uint64_t) physbase / PAGE_SIZE; i < ((uint64_t)physfree /PAGE_SIZE); i++) 38 | set_mem_bit(i); 39 | 40 | setup_page_tables(physfree); 41 | 42 | } 43 | //Function for setting up page tables 44 | void setup_page_tables(char* physfree) 45 | { 46 | 47 | page_dir *pg_dir = (page_dir *)kmalloc(sizeof(page_dir), physfree, 1); 48 | // print("\nAddress : %x", pg_dir); 49 | 50 | pg_tbl1 *pg_tbl1_i = (pg_tbl1 *)kmalloc(sizeof(pg_tbl1), physfree, 1); 51 | // print("\nAddress : %x", pg_tbl1_i); 52 | 53 | pg_tbl2 *pg_tbl2_i = (pg_tbl2 *)kmalloc(sizeof(pg_tbl2), physfree, 1); 54 | // print("\nAddress : %x", pg_tbl2_i); 55 | 56 | pg_tbl3 *pg_tbl3_i = (pg_tbl3 *)kmalloc(sizeof(pg_tbl3), physfree, 1); 57 | //print("\nAddress : %x", pg_tbl3_i); 58 | 59 | set_pg_dir(pg_dir, 511, (uint64_t)pg_tbl1_i); 60 | set_pg_tbl1(pg_tbl1_i, 510, (uint64_t)pg_tbl2_i); 61 | set_pg_tbl2(pg_tbl2_i, 1, (uint64_t)pg_tbl3_i); 62 | 63 | /* pg_tbl1 *pg_tbl1_p = (pg_tbl1 *)kmalloc(sizeof(pg_tbl1), physfree, 1); 64 | print("\nAddress : %x", pg_tbl1_p); 65 | */ 66 | /*pg_tbl2 *pg_tbl2_p = (pg_tbl2 *)kmalloc(sizeof(pg_tbl2), physfree, 1); 67 | print("\nAddress : %x", pg_tbl2_p); 68 | */ 69 | pg_tbl3 *pg_tbl3_p = (pg_tbl3 *)kmalloc(sizeof(pg_tbl3), physfree, 1); 70 | //print("\nAddress : %x", pg_tbl3_p); 71 | 72 | //set_pg_dir(pg_dir, 511, (uint64_t)pg_tbl1_p); 73 | //set_pg_tbl1(pg_tbl1_i, 508, (uint64_t)pg_tbl2_p); 74 | set_pg_tbl2(pg_tbl2_i, 0, (uint64_t)pg_tbl3_p); 75 | 76 | /*pg_tbl1_p->pg_tbl1_ptr[0] = 0; 77 | pg_tbl2_p->pg_tbl2_ptr[0] = 0; 78 | pg_tbl3_p->pg_tbl3_ptr[0] = 0;*/ 79 | uint64_t physbase_t = 0x200000; 80 | uint64_t physfree_t = 0x20b000; 81 | 82 | 83 | 84 | //i++; 85 | //print(" v %d", i); 86 | 87 | physbase_t = 0x200000; 88 | physfree_t = 0x20b000; 89 | int i =0; 90 | //print("\n"); 91 | for (; physbase_t <= physfree_t; physbase_t += 0x1000) 92 | { 93 | set_pg_tbl3(pg_tbl3_i, i, physbase_t); 94 | i++; 95 | print(" %d", i); 96 | } 97 | 98 | physbase_t = 0xB8000; 99 | physfree_t = 0xBa000; 100 | i=184; 101 | for (; physbase_t <= physfree_t; physbase_t += 0x1000) 102 | { 103 | 104 | set_pg_tbl3(pg_tbl3_p, i, physbase_t); 105 | i++; 106 | } 107 | //set_pg_tbl3(&pg_tbl3_i, 0, (uint64_t)0x200000); 108 | 109 | switch_page_directory(pg_dir); 110 | } 111 | 112 | void switch_page_directory(page_dir *dir) 113 | { 114 | //print("\n%x", &dir->pg_dir_ptr[0]); 115 | load_cr3(dir ); 116 | 117 | change_video_pointer(); 118 | print("Printing after remapping video memory"); 119 | // current_directory = dir; 120 | /* print("\n%x", &dir); 121 | print("\n%x", &dir->pg_dir_ptr); 122 | print("\n%x", &dir->pg_dir_ptr[0]); 123 | print("\n%x", &dir->pg_dir_ptr[511]);*/ 124 | //print("\n%x", ((*dir).pg_dir_ptr)[511]); 125 | 126 | /* asm volatile("movq %%cr3, %0":: "b"(((uint64_t)&dir->pg_dir_ptr) << 12)); 127 | unsigned int cr0; 128 | asm volatile("movq %0, %%cr0": "=b"(cr0)); 129 | cr0 |= 0x80000000; 130 | asm volatile("movq %%cr0, %0":: "b"(cr0)); 131 | 132 | unsigned int cr4; 133 | asm volatile("mov %%cr0, %0": "=b"(cr4)); 134 | cr4 |= 0x5; 135 | asm volatile("mov %0, %%cr0":: "b"(cr4)); 136 | */ 137 | /*asm volatile ("movl %0, %%cr3" :: "a" (((uint64_t)&dir->pg_dir_ptr) << 12));*/ 138 | //asm volatile ("mov %cr4, %eax; bts $5, %eax; movl %eax, %cr4"); // set bit5 in CR4 to enable PAE 139 | //asm volatile ("mov %%eax, %%cr3" :: "a" (((uint64_t)&dir->pg_dir_ptr) << 12)); 140 | //asm volatile ("mov %cr0, %eax; orl $0x80000000, %eax; movl %eax, %cr0;"); 141 | 142 | // asm volatile("mov %0, %%cr3":: "r"()); 143 | // uint64_t cr0; 144 | // asm volatile("mov %%cr0, %0": "=r"(cr0)); 145 | // cr0 |= 0x80000000; // Enable paging! 146 | // asm volatile("mov %0, %%cr0":: "r"(cr0)); 147 | } 148 | 149 | 150 | void set_pg_dir(page_dir *p_dir, uint64_t index, uint64_t value) 151 | { 152 | uint64_t flags = 0x007; 153 | uint64_t address = value; 154 | address = address | flags; 155 | p_dir->pg_dir_ptr[index] = address; 156 | } 157 | 158 | void set_pg_tbl1(pg_tbl1 *pg_tbl, uint64_t index, uint64_t value) 159 | { 160 | uint64_t flags = 0x007; 161 | uint64_t address = value; 162 | address = address | flags; 163 | 164 | pg_tbl->pg_tbl1_ptr[index] = address; 165 | } 166 | 167 | void set_pg_tbl2(pg_tbl2 *pg_tbl, uint64_t index, uint64_t value) 168 | { 169 | uint64_t flags = 0x007; 170 | uint64_t address = value; 171 | address = address | flags; 172 | 173 | pg_tbl->pg_tbl2_ptr[index] = address; 174 | } 175 | 176 | void set_pg_tbl3(pg_tbl3 *pg_tbl, uint64_t index, uint64_t value) 177 | { 178 | uint64_t flags = 0x007; 179 | uint64_t address = value; 180 | address = address | flags; 181 | 182 | pg_tbl->pg_tbl3_ptr[index] = address; 183 | } 184 | 185 | uint64_t kmalloc(uint64_t sz, char * physfree, int align) 186 | { 187 | uint64_t phys_addr = (uint64_t)physfree; 188 | while(get_mem_bit(phys_addr/PAGE_SIZE) != AVAILABLE) 189 | { 190 | phys_addr += PAGE_SIZE; 191 | } 192 | 193 | set_mem_bit((uint64_t)phys_addr/PAGE_SIZE); 194 | 195 | uint64_t placement_address = (uint64_t)phys_addr; 196 | if (align == 1 && (placement_address & (uint64_t)0xFFFFF000)) // If the address is not already page-aligned 197 | { 198 | // Align it. 199 | placement_address &= 0xFFFFF000; 200 | placement_address += 0x1000; 201 | } 202 | uint64_t address = placement_address; 203 | 204 | return address; 205 | } 206 | 207 | int get_phy_page() 208 | { 209 | return 0; 210 | } 211 | 212 | void set_mem_bit(uint64_t position) 213 | { 214 | phy_mem_map[position / 8] |= (1 << (position % 8)); 215 | } 216 | 217 | int get_mem_bit(uint64_t position) 218 | { 219 | return (phy_mem_map[position / 8] >> (position % 8)) & 1; 220 | } 221 | 222 | void unset_mem_bit(int position) 223 | { 224 | phy_mem_map[position / 8] &= ~ (1 << (position % 8)); 225 | } 226 | 227 | -------------------------------------------------------------------------------- /sys/vmem.s: -------------------------------------------------------------------------------- 1 | .data 2 | 3 | .global load_cr3 4 | load_cr3: 5 | movq %rdi, %rax 6 | movq %rax, %cr3 7 | retq 8 | -------------------------------------------------------------------------------- /tarfs: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/rajesh5310/SBUnix/441268cc45d5f133ca9cbe9566d462320303d86e/tarfs -------------------------------------------------------------------------------- /upload/assert.h: -------------------------------------------------------------------------------- 1 | /* See COPYRIGHT for copyright information. */ 2 | 3 | #ifndef JOS_INC_ASSERT_H 4 | #define JOS_INC_ASSERT_H 5 | 6 | #include 7 | 8 | void _warn(const char*, int, const char*, ...); 9 | void _panic(const char*, int, const char*, ...) __attribute__((noreturn)); 10 | 11 | #define warn(...) _warn(__FILE__, __LINE__, __VA_ARGS__) 12 | #define panic(...) _panic(__FILE__, __LINE__, __VA_ARGS__) 13 | 14 | #define assert(x) \ 15 | do { if (!(x)) panic("assertion failed: %s", #x); } while (0) 16 | 17 | // static_assert(x) will generate a compile-time error if 'x' is false. 18 | #define static_assert(x) switch (x) case 0: case (x): 19 | 20 | #endif /* !JOS_INC_ASSERT_H */ 21 | -------------------------------------------------------------------------------- /upload/pmap.h: -------------------------------------------------------------------------------- 1 | /* See COPYRIGHT for copyright information. */ 2 | 3 | #ifndef JOS_KERN_PMAP_H 4 | #define JOS_KERN_PMAP_H 5 | #ifndef JOS_KERNEL 6 | # error "This is a JOS kernel header; user programs should not #include it" 7 | #endif 8 | 9 | #include 10 | #include 11 | struct Env; 12 | 13 | extern char bootstacktop[], bootstack[]; 14 | 15 | extern struct Page *pages; 16 | extern size_t npages; 17 | 18 | extern pml4e_t *boot_pml4e; 19 | 20 | 21 | /* This macro takes a kernel virtual address -- an address that points above 22 | * KERNBASE, where the machine's maximum 256MB of physical memory is mapped -- 23 | * and returns the corresponding physical address. It panics if you pass it a 24 | * non-kernel virtual address. 25 | */ 26 | #define PADDR(kva) \ 27 | ({ \ 28 | physaddr_t __m_kva = (physaddr_t) (kva); \ 29 | if (__m_kva < KERNBASE) \ 30 | panic("PADDR called with invalid kva %08lx", __m_kva);\ 31 | __m_kva - KERNBASE; \ 32 | }) 33 | 34 | /* This macro takes a physical address and returns the corresponding kernel 35 | * virtual address. It panics if you pass an invalid physical address. */ 36 | #define KADDR(pa) \ 37 | ({ \ 38 | physaddr_t __m_pa = (pa); \ 39 | uint32_t __m_ppn = PPN(__m_pa);\ 40 | if (__m_ppn >= npages) \ 41 | panic("KADDR called with invalid pa %08lx", __m_pa);\ 42 | (void*) ((uint64_t)(__m_pa + KERNBASE)); \ 43 | }) 44 | 45 | 46 | enum { 47 | // For page_alloc, zero the returned physical page. 48 | ALLOC_ZERO = 1<<0, 49 | }; 50 | 51 | void x64_vm_init(); 52 | 53 | void page_init(void); 54 | struct Page * page_alloc(int alloc_flags); 55 | void page_free(struct Page *pp); 56 | int page_insert(pml4e_t *pml4e, struct Page *pp, void *va, int perm); 57 | void page_remove(pml4e_t *pml4e, void *va); 58 | struct Page *page_lookup(pml4e_t *pml4e, void *va, pte_t **pte_store); 59 | void page_decref(struct Page *pp); 60 | 61 | void tlb_invalidate(pml4e_t *pml4e, void *va); 62 | 63 | void * mmio_map_region(physaddr_t pa, size_t size); 64 | 65 | int user_mem_check(struct Env *env, const void *va, size_t len, int perm); 66 | void user_mem_assert(struct Env *env, const void *va, size_t len, int perm); 67 | 68 | static inline ppn_t 69 | page2ppn(struct Page *pp) 70 | { 71 | return pp - pages; 72 | } 73 | 74 | static inline physaddr_t 75 | page2pa(struct Page *pp) 76 | { 77 | return page2ppn(pp) << PGSHIFT; 78 | } 79 | 80 | static inline struct Page* 81 | pa2page(physaddr_t pa) 82 | { 83 | if (PPN(pa) >= npages) 84 | panic("pa2page called with invalid pa"); 85 | return &pages[PPN(pa)]; 86 | } 87 | 88 | static inline void* 89 | page2kva(struct Page *pp) 90 | { 91 | return KADDR(page2pa(pp)); 92 | } 93 | 94 | pte_t *pgdir_walk(pde_t *pgdir, const void *va, int create); 95 | 96 | pte_t *pml4e_walk(pml4e_t *pml4e, const void *va, int create); 97 | 98 | pde_t *pdpe_walk(pdpe_t *pdpe,const void *va,int create); 99 | 100 | #endif /* !JOS_KERN_PMAP_H */ 101 | -------------------------------------------------------------------------------- /upload/types.h: -------------------------------------------------------------------------------- 1 | #ifndef JOS_INC_TYPES_H 2 | #define JOS_INC_TYPES_H 3 | 4 | #ifndef NULL 5 | #define NULL ((void*) 0) 6 | #endif 7 | 8 | // Represents true-or-false values 9 | typedef _Bool bool; 10 | enum { false, true }; 11 | 12 | // Explicitly-sized versions of integer types 13 | typedef __signed char int8_t; 14 | typedef unsigned char uint8_t; 15 | typedef short int16_t; 16 | typedef unsigned short uint16_t; 17 | typedef int int32_t; 18 | typedef unsigned int uint32_t; 19 | typedef long long int64_t; 20 | typedef unsigned long long uint64_t; 21 | 22 | // Pointers and addresses are 32 bits long. 23 | // We use pointer types to represent virtual addresses, 24 | // uintptr_t to represent the numerical values of virtual addresses, 25 | // and physaddr_t to represent physical addresses. 26 | typedef int32_t intptr_t; 27 | typedef uint64_t uintptr_t; 28 | typedef uint64_t physaddr_t; 29 | 30 | // Page numbers are 32 bits long. 31 | typedef uint64_t ppn_t; 32 | 33 | // size_t is used for memory object sizes. 34 | typedef uint64_t size_t; 35 | // ssize_t is a signed version of ssize_t, used in case there might be an 36 | // error return. 37 | typedef int32_t ssize_t; 38 | 39 | // off_t is used for file offsets and lengths. 40 | typedef int32_t off_t; 41 | 42 | // Efficient min and max operations 43 | #define MIN(_a, _b) \ 44 | ({ \ 45 | typeof(_a) __a = (_a); \ 46 | typeof(_b) __b = (_b); \ 47 | __a <= __b ? __a : __b; \ 48 | }) 49 | #define MAX(_a, _b) \ 50 | ({ \ 51 | typeof(_a) __a = (_a); \ 52 | typeof(_b) __b = (_b); \ 53 | __a >= __b ? __a : __b; \ 54 | }) 55 | 56 | // Rounding operations (efficient when n is a power of 2) 57 | // Round down to the nearest multiple of n 58 | #define ROUNDDOWN(a, n) \ 59 | ({ \ 60 | uint64_t __a = (uint64_t) (a); \ 61 | (typeof(a)) (__a - __a % (n)); \ 62 | }) 63 | // Round up to the nearest multiple of n 64 | #define ROUNDUP(a, n) \ 65 | ({ \ 66 | uint64_t __n = (uint64_t) (n); \ 67 | (typeof(a)) (ROUNDDOWN((uint64_t) (a) + __n - 1, __n)); \ 68 | }) 69 | 70 | // Return the offset of 'member' relative to the beginning of a struct type 71 | #define offsetof(type, member) ((size_t) (&((type*)0)->member)) 72 | 73 | #endif /* !JOS_INC_TYPES_H */ 74 | --------------------------------------------------------------------------------