#include <stdio.h>
#include <types.h>
#include <string.h>
-#define CMD_SIZE 256
-char cmd[CMD_SIZE];
-
-void get_cmd()
-{
-#if 0
- int i;
-
- i=0;
- printf("#");
- while(1)
- {
- int k;
- char ch;
- extern char ParseKbdInput(int k);
- extern unsigned char read_kbd();
-reinput:
- k = read_kbd();
- ch = ParseKbdInput(k);
- if(ch == -1)
- continue;
-
- if(ch == '\b')
- {
- i = --i < 0 ? 0 : i;
- cmd[i] = 0;
- continue;
- }
-
- if(ch == '\n')
- {
- cmd[i++] = 0;
- break;
- }
-
- cmd[i++] = ch;
-
- if(i == CMD_SIZE - 1)
- {
- printf("shell buffer is full..."
- "reset buffer...\n");
- i = 0;
- }
- }
-
- int len = strlen(cmd);
- int j;
- int flag = 0;
- for(j = len - 1; j>=0; j--) {
- if(cmd[j] != '\n') {
- flag = 1;
- break;
- }
- }
-
- if(flag == 0) {
- i = 0;
- goto reinput;
- }
-
- cmd[CMD_SIZE-1] = 0;
-#endif
-}
+int systest();
int main()
{
- char buf[CMD_SIZE];
-
while(1)
{
- get_cmd();
-
- //printf("\nCMD: %s\n", cmd);
-
- if(cmd[0] == 0) continue;
- if(cmd[0] != '/')
- {
- strcpy(buf, cmd);
- strcpy(cmd, "/bin/");
- strcat(cmd, buf);
- }
-
- pid_t pid;
- pid = fork();
-
- if(pid<0)
- {
- printf("shit happens in shell\n");
- while(1);
- }
- else if(pid == 0)
- {
- execv(cmd, NULL);
- }
-
- int i = 100000;
- while(i--);
+ systest();
+ asm("nop;nop;nop;");
}
-
-
return 0;
}
ext2_free_block(blk);
}
-unsigned int ext2_search_indir(const char *name, const ext2_inode_t *inode, unsigned int io)
+void ext2_read_file(const ext2_inode_t *inode, char *buf)
+{
+ assert(inode != 0);
+ assert(inode->i_size > 0 && inode->i_size<=MAX_SUPT_FILE_SIZE);
+
+ unsigned int blkcnt = inode->i_size / EXT2_BLOCK_SIZE;
+ int i;
+ for(i=0; i<blkcnt; ++i)
+ {
+ BLKRW(inode->i_block[i], 1, buf+i*EXT2_BLOCK_SIZE);
+ printk("read block\n");
+ }
+
+ unsigned int left = inode->i_size % EXT2_BLOCK_SIZE;
+ if(left)
+ {
+ void *blk = ext2_alloc_block();
+
+ memcpy(buf+i*EXT2_BLOCK_SIZE, blk, left);
+
+ ext2_free_block(blk);
+ }
+}
+
+unsigned int ext2_search_indir(const char *name, const ext2_inode_t *inode)
{
unsigned int ino = 0;
void *blk = ext2_alloc_block();
assert(blk != 0);
-
- printk("ext2_search_indir inode no %u search name %s block %u\n", io, name, inode->i_block[0]);
-
BLKRW(inode->i_block[0], 1, blk); // only support the first direct blocks
ext2_dirent_t *dirent = (ext2_dirent_t *) blk;
while((len=get_filename_from_path(path, file)) != 0)
{
printk("name len %u\n", len);
- ino = ext2_search_indir(file, inode, ino);
+ ino = ext2_search_indir(file, inode);
assert(ino != 0);
printk("FILE:%s inode %u\n", file, ino);
}
ext2_read_inode(2, &ext2_root_inode);
-#if 0
+#if 1
printk("root inode.i_size %u \n", ext2_root_inode.i_size);
printk("root blocks %u \n", ext2_root_inode.i_blocks);
{
ext2_setup_fs();
}
+
+unsigned int namei(const char *path)
+{
+ return ext2_search_inpath(path);
+}
#define EXT2_NAME_LEN 255
typedef struct ext2_dir_ent
{
- u32 inode;
- u16 rec_len;
- u8 name_len;
- u8 file_type; /* 目录类型 */
+ u32 inode;
+ u16 rec_len;
+ u8 name_len;
+ u8 file_type;
char name[EXT2_NAME_LEN];
} ext2_dirent_t;
#define EXT2_MAX_REC_LEN ((1<<16)-1)
+void ext2_read_inode(unsigned int ino, ext2_inode_t *inode);
+void ext2_read_file(const ext2_inode_t *inode, char *buf);
#endif //_EXT2_H
-#define MAX_SUPT_FILE_SIZE (1)
+//#define MAX_SUPT_FILE_SIZE (1)
#define NR_FILES (1)
#define NR_OPENS (1)
-#if 0
+unsigned int namei(const char *path);
+
#define MAX_SUPT_FILE_SIZE (EXT2_IND_BLOCK*EXT2_BLOCK_SIZE)
+#if 0
#define NR_FILES (PAGE_SIZE/sizeof(File))
#define NR_INODES (2*NR_FILES)
#define NR_OPENS (2) /* 一个进程同时打开文件的限制数 */
MPL_ROOT,
MPL_TASK_1,
MPL_TASK_2,
+ MPL_TEST,
MPL_END
};
#include <assert.h>
#include <stat.h>
#include <sched.h>
-#include <memory.h>
+#include <mm.h>
#include <elf.h>
+#include <fs.h>
+#include <ext2.h>
int sysc_exec(const char *path, char *const argv[])
{
assert(argv == NULL); // unsupport now
- int fd;
- int i;
+ unsigned int ino = namei(path);
+ if(ino == 0)
+ return -ENOENT;
- fd = sysc_open(path, O_RDONLY, 0);
- if(fd == -1)
- {
- panic("can not find file");
- }
+ ext2_inode_t inode;
- int filesz;
- Stat stat;
- char *buf;
+ ext2_read_inode(ino, &inode);
- sysc_stat(fd, &stat);
- filesz = stat.st_size;
- if(stat.st_size <=0 || stat.st_size>MAX_SUPT_FILE_SIZE)
- {
- printk("file %s is not exist\n", path);
- return -ENOENT;
- }
- buf = (void*)kmalloc_old(filesz);
- sysc_read(fd, buf, filesz);
+ //void *buf = (void*)kmalloc(inode.i_size, 0);
+ void *buf = (void *) alloc_pages(0, 5);
+ assert(buf != 0);
+
+ ext2_read_file(&inode, buf);
-#if 0
- for(i=0; i<filesz; i++)
- printk("%02x ", (unsigned char)buf[i]);
-#endif
pElf32_Ehdr ehdr = (pElf32_Ehdr) buf;
//assert(strncmp(ELFMAG, ehdr->e_ident, sizeof(ELFMAG)-1) == 0);
if(strncmp(ELFMAG, ehdr->e_ident, sizeof(ELFMAG)-1) != 0)
{
printk("file %s can not execute\n", path);
- kfree_old(buf);
+ kfree(buf);
return -ENOEXEC;
}
//printk("Entry: %08x phnum:%d\n", ehdr->e_entry, ehdr->e_phnum);
int size = 0;
char *pv = NULL; // phdr 中第一个的VirtAddr
+ int i;
for(i=0; i<ehdr->e_phnum; i++)
{
pElf32_Phdr phdr;
}
}
- char *exe = (char *) kmalloc_old(size);
+ char *exe = (char *) kmalloc(size, 0);
for(i=0; i<ehdr->e_phnum; i++)
{
pElf32_Phdr phdr;
* 即12K~48K之间
* 所以就以一个页目录项来简化处理
*/
- u32 *pd = (u32*) pa2va(current->cr3);
+ u32 *pd = (u32*) current->cr3;
u32 *pt;
u32 pa_exe;
u32 npd, npt;
- pa_exe = va2pa(exe);
- npd = get_npd(ehdr->e_entry);
- pt = get_phys_pages(1);
+ pa_exe = va2pa(exe);
+ npd = get_npd(ehdr->e_entry);
+ pt = (u32*)va2pa(alloc_one_page(0));
if(pt == NULL)
panic("out of memory");
+
//printk("npd: %d pt:%08x\n", npd, pt);
memset(pa2va(pt), 0, PAGE_SIZE);
pd[npd] = (u32) pt | 7;
//printk("exe : %08x cr3:%08x\n", exe, pd);
-
/* 准备内核栈的数据并从ret_from_fork返回 */
pt_regs_t * regs = ((pt_regs_t *)(TASK_SIZE+(unsigned long)current)) - 1;
extern void ret_from_fork_user();
regs->eip = (unsigned long)ehdr->e_entry;
current->esp = (unsigned long) regs;
current->eip = (unsigned long)ret_from_fork_user;
+ *((unsigned long *)regs->esp) = (unsigned long)ehdr->e_entry;
-#if 0 /* 写完之后发现貌似不用 */
- /* 准备用户栈数据 */
- /* 先找到用户栈的位置 */
- u32 pde = pd[get_npd(KRNLADDR)-1] & PAGE_MASK;
- pt = pa2va(pde);
- u32 *stack = (u32*)pa2va(pt[1023]);
- stack[1023] = 0x00;
- stack[1022] = 0x00; /* ebp */
- stack[1021] = 0x00; /* edx */
- stack[1020] = 0x00; /* ecx */
- printk("stack pt: %08x pde:%08x %08x %08x\n",
- pt, pde, pd[get_npd(KRNLADDR)-1]);
-#endif
- kfree_old(buf);
-
+ //kfree(buf);
//printk("eip: %08x \n", regs->eip);
- load_cr3(current);
+ //load_cr3(current);
return 0;
}
int cnt = 0;
pid_t id = sysc_getpid();
+ if(id == 1)
+ {
+ int r = sysc_exec("/bin/shell", 0);
+ }
+
while(1)
{
printl(MPL_TASK_1+id-1, "task:%d [%08x] weight %d cnt %d", id, current, current->weight, cnt++);
void root_task_entry()
{
kernel_task(init_task_entry);
- kernel_task(init_task_entry);
+ //kernel_task(init_task_entry);
int cnt = 0;
while(1)
return 0;
}
-int sysc_test() { }
+int sysc_test()
+{
+ static unsigned int cnt;
+ printl(MPL_TEST, "sysc_test %u", cnt++);
+}
void init_sysc_handler_table()
{
{
return syscall2(SYSC_EXEC, path, argv);
}
+
+int systest()
+{
+ return syscall0(SYSC_TEST);
+}