/* disable kernel mode cache */ mfc0 t0, CP0_CONFIG and t0, ~0x7 ori t0, 0x2 mtc0 t0, CP0_CONFIG
/* Exercise 1.4. Please complete this part. To do: set up stack. hint: you can reference the memory layout in the include/mmu.h. call main func. */ /* ans begin*/ li sp, 0x80400000 li t0, 0x80400000 #sw t0, mCONTEXT jal main
/*ans end*/
loop: j loop nop END(_start) /*the function defined in asm.h*/
/* * Copyright (C) 2001 MontaVista Software Inc. * Author: Jun Sun, jsun@mvista.com or jsun@junsun.net * * This program is free software; you can redistribute it and/or modify it * under the terms of the GNU General Public License as published by the * Free Software Foundation; either version 2 of the License, or (at your * option) any later version. * */
#include<printf.h> #include<pmap.h>
intmain() { printf("main.c:\tmain is start ...\n");
/* This file defines standard ELF types, structures, and macros. Copyright (C) 1995, 1996, 1997, 1998, 1999 Free Software Foundation, Inc. This file is part of the GNU C Library. Contributed by Ian Lance Taylor <ian@cygnus.com>. The GNU C Library is free software; you can redistribute it and/or modify it under the terms of the GNU Library General Public License as published by the Free Software Foundation; either version 2 of the License, or (at your option) any later version. The GNU C Library is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Library General Public License for more details. You should have received a copy of the GNU Library General Public License along with the GNU C Library; see the file COPYING.LIB. If not, write to the Free Software Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */
#ifndef _KER_ELF_H #define _KER_ELF_H
/* ELF defination file from GNU C Library. We simplefied this * file for our lab, removing definations about ELF64, structs and * enums which we don't care. */
/* Type for a 16-bit quantity. */ typedefuint16_t Elf32_Half;
/* Types for signed and unsigned 32-bit quantities. */ typedefuint32_t Elf32_Word; typedefint32_t Elf32_Sword;
/* Types for signed and unsigned 64-bit quantities. */ typedefuint64_t Elf32_Xword; typedefint64_t Elf32_Sxword;
/* Type of addresses. */ typedefuint32_t Elf32_Addr;
/* Type of file offsets. */ typedefuint32_t Elf32_Off;
/* Type for section indices, which are 16-bit quantities. */ typedefuint16_t Elf32_Section;
/* Type of symbol indices. */ typedefuint32_t Elf32_Symndx;
/* The ELF file header. This appears at the start of every ELF file. */ /* ELF 文件的文件头。所有的ELF文件均以此为起始 */ #define EI_NIDENT (16)
typedefstruct { unsignedchar e_ident[EI_NIDENT]; /* Magic number and other info */ // 存放魔数以及其他信息 Elf32_Half e_type; /* Object file type */ // 文件类型 Elf32_Half e_machine; /* Architecture */ // 机器架构 Elf32_Word e_version; /* Object file version */ // 文件版本 Elf32_Addr e_entry; /* Entry point virtual address */ // 入口点的虚拟地址 Elf32_Off e_phoff; /* Program header table file offset */ // 程序头表所在处与此文件头的偏移 Elf32_Off e_shoff; /* Section header table file offset */ // 节头表所在处与此文件头的偏移,ELF文件头地址加上该偏移即为节头表第一项地址 Elf32_Word e_flags; /* Processor-specific flags */ // 针对处理器的标记 Elf32_Half e_ehsize; /* ELF header size in bytes */ // ELF文件头的大小(单位为字节) Elf32_Half e_phentsize; /* Program header table entry size */ // 程序头表入口大小 Elf32_Half e_phnum; /* Program header table entry count */ // 程序头表入口数 Elf32_Half e_shentsize; /* Section header table entry size */ // 节头表入口大小 Elf32_Half e_shnum; /* Section header table entry count */ // 节头表入口数 Elf32_Half e_shstrndx; /* Section header string table index */ // 节头字符串编号 } Elf32_Ehdr; typedefstruct { // section name Elf32_Word sh_name; // section type Elf32_Word sh_type; // section flags Elf32_Word sh_flags; // section addr Elf32_Addr sh_addr; // offset from elf head of this entry Elf32_Off sh_offset; // byte size of this section Elf32_Word sh_size; // link Elf32_Word sh_link; // extra info Elf32_Word sh_info; // alignment Elf32_Word sh_addralign; // entry size Elf32_Word sh_entsize; }Elf32_Shdr;
typedefstruct { // segment type Elf32_Word p_type; // offset from elf file head of this entry Elf32_Off p_offset; // virtual addr of this segment Elf32_Addr p_vaddr; // physical addr, in linux, this value is meanless and has same value of p_vaddr Elf32_Addr p_paddr; // file size of this segment Elf32_Word p_filesz; // memory size of this segment Elf32_Word p_memsz; // segment flag Elf32_Word p_flags; // alignment Elf32_Word p_align; }Elf32_Phdr;
/* Legal values for p_type (segment type). */
#define PT_NULL 0 /* Program header table entry unused */ #define PT_LOAD 1 /* Loadable program segment */ #define PT_DYNAMIC 2 /* Dynamic linking information */ #define PT_INTERP 3 /* Program interpreter */ #define PT_NOTE 4 /* Auxiliary information */ #define PT_SHLIB 5 /* Reserved */ #define PT_PHDR 6 /* Entry for header table itself */ #define PT_NUM 7 /* Number of defined types. */ #define PT_LOOS 0x60000000 /* Start of OS-specific */ #define PT_HIOS 0x6fffffff /* End of OS-specific */ #define PT_LOPROC 0x70000000 /* Start of processor-specific */ #define PT_HIPROC 0x7fffffff /* End of processor-specific */
/* This is a simplefied ELF reader. * You can contact me if you find any bugs. * * Luming Wang<wlm199558@126.com> */
#include"kerelf.h" #include<stdio.h> /* Overview: * Check whether it is a ELF file. * * Pre-Condition: * binary must longer than 4 byte. * * Post-Condition: * Return 0 if `binary` isn't an elf. Otherwise * return 1. */ intis_elf_format(u_char *binary) { // 对魔数判等,若不等则说明不是elf格式文件 Elf32_Ehdr *ehdr = (Elf32_Ehdr *)binary; if (ehdr->e_ident[EI_MAG0] == ELFMAG0 && ehdr->e_ident[EI_MAG1] == ELFMAG1 && ehdr->e_ident[EI_MAG2] == ELFMAG2 && ehdr->e_ident[EI_MAG3] == ELFMAG3) { return1; }
return0; }
/* Overview: * read an elf format binary file. get ELF's information * * Pre-Condition: * `binary` can't be NULL and `size` is the size of binary. * * Post-Condition: * Return 0 if success. Otherwise return < 0. * If success, output address of every section in ELF. */
externintreadelf(u_char* binary, intsize); /* overview: input a elf format file name from control line, call the readelf function to parse it. params: argc: the number of parameters argv: array of parameters, argv[1] shuold be the file name. */ intmain(int argc,char *argv[]) { FILE* fp; int fsize; unsignedchar *p;
#include"types.h" /* * This file contains: * * Part 1. MIPS definitions. * Part 2. Our conventions. * Part 3. Our helper functions. */
/* * Part 1. MIPS definitions. */ #define BY2PG 4096 // bytes to a page #define PDMAP (4*1024*1024) // bytes mapped by a page directory entry #define PGSHIFT 12 #define PDX(va) ((((u_long)(va))>>22) & 0x03FF) #define PTX(va) ((((u_long)(va))>>12) & 0x03FF) #define PTE_ADDR(pte) ((u_long)(pte)&~0xFFF)
// page number field of address #define PPN(va) (((u_long)(va))>>12) #define VPN(va) PPN(va)
#define VA2PFN(va) (((u_long)(va)) & 0xFFFFF000 ) // va 2 PFN for EntryLo0/1 //$#define VA2PDE(va) (((u_long)(va)) & 0xFFC00000 ) // for context /* Page Table/Directory Entry flags * these are defined by the hardware */ #define PTE_G 0x0100 // Global bit #define PTE_V 0x0200 // Valid bit #define PTE_R 0x0400 // Dirty bit ,'0' means only read ,otherwise make interrupt #define PTE_UC 0x0800 // unCached
/* * Part 2. Our conventions. */
/* o 4G -----------> +----------------------------+------------0x100000000 o | ... | kseg3 o +----------------------------+------------0xe000 0000 o | ... | kseg2 o +----------------------------+------------0xc000 0000 o | Interrupts & Exception | kseg1 o +----------------------------+------------0xa000 0000 o | Invalid memory | /|\ o +----------------------------+----|-------Physics Memory Max o | ... | kseg0 o VPT,KSTACKTOP-----> +----------------------------+----|-------0x8040 0000-------end o | Kernel Stack | | KSTKSIZE /|\ o +----------------------------+----|------ | o | Kernel Text | | PDMAP o KERNBASE -----> +----------------------------+----|-------0x8001 0000 | o | Interrupts & Exception | \|/ \|/ o ULIM -----> +----------------------------+------------0x8000 0000------- o | User VPT | PDMAP /|\ o UVPT -----> +----------------------------+------------0x7fc0 0000 | o | PAGES | PDMAP | o UPAGES -----> +----------------------------+------------0x7f80 0000 | o | ENVS | PDMAP | o UTOP,UENVS -----> +----------------------------+------------0x7f40 0000 | o UXSTACKTOP -/ | user exception stack | BY2PG | o +----------------------------+------------0x7f3f f000 | o | Invalid memory | BY2PG | o USTACKTOP ----> +----------------------------+------------0x7f3f e000 | o | normal user stack | BY2PG | o +----------------------------+------------0x7f3f d000 | a | | | a ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ | a . . | a . . kuseg a . . | a |~~~~~~~~~~~~~~~~~~~~~~~~~~~~| | a | | | o UTEXT -----> +----------------------------+ | o | | 2 * PDMAP \|/ a 0 ------------> +----------------------------+ ----------------------------- o */
/* * Copyright (C) 2001 MontaVista Software Inc. * Author: Jun Sun, jsun@mvista.com or jsun@junsun.net * * This program is free software; you can redistribute it and/or modify it * under the terms of the GNU General Public License as published by the * Free Software Foundation; either version 2 of the License, or (at your * option) any later version. * */
This is copyright.