Skip to content

Commit

Permalink
video done
Browse files Browse the repository at this point in the history
  • Loading branch information
YourBroDuke committed Jan 13, 2019
1 parent 9c3cdba commit 38638f7
Show file tree
Hide file tree
Showing 10 changed files with 70 additions and 69 deletions.
3 changes: 2 additions & 1 deletion include/zjunix/pc.h
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,7 @@ extern struct list_head tasks; //存放所有进程
extern struct list_head sched[PRORITY_NUM + 1]; //调度链表
extern task_struct *current_task; //当前进程
unsigned char pro_map[PRORITY_BYTES]; //优先级位图
// int argsc = 0;

void init_pc_list();
void init_pro_map();
Expand Down Expand Up @@ -100,7 +101,7 @@ void add_terminal(task_struct * task);
void task_files_delete(task_struct * task);
int pc_kill(pid_t pid);
int kernel_proc(unsigned int argc, void * argv);
int exec_kernel(void *argv, int is_wait, int is_user);
int exec_kernel(int argc, void *argv, int is_wait, int is_user);
task_struct * wait_check(pid_t pid);
void task_exit();
void wakeup_parent();
Expand Down
3 changes: 2 additions & 1 deletion kernel/init.c
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
#include <zjunix/bootmm.h>
#include <zjunix/buddy.h>
#include <zjunix/fs/fat.h>
#include <zjunix/mfs/fat32.h>
#include <zjunix/log.h>
#include <zjunix/pc.h>
#include <zjunix/slab.h>
Expand Down Expand Up @@ -68,7 +69,7 @@ void init_kernel() {
log(LOG_END, "Memory Modules.");
// File system
log(LOG_START, "File System.");
init_fs();
init_fat32();
log(LOG_END, "File System.");
// System call
log(LOG_START, "System Calls.");
Expand Down
18 changes: 2 additions & 16 deletions kernel/mfs/dir.c
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ DIR * opendir(u8 *path) {
ans->crt_index = 0;
return ans;
}
else if (path[0] == '/') {
else {
MY_FILE *myfile;
fat32_open(myfile, path);
ans->start_clus = get_start_clu_num(myfile);
Expand Down Expand Up @@ -52,11 +52,6 @@ dirent *readdir(DIR *dir) {

while (crt_clu != 0x0FFFFFFF) {

#ifdef FS_DEBUG
disk_name_str[11] = 0;
kernel_printf("The current searching is %s\n", disk_name_str);
#endif

// Traverse every sector in current cluster
for (u32 i = 0; i < SEC_PER_CLU; i++) {
// Traverse every dentry in current sector
Expand All @@ -83,15 +78,6 @@ dirent *readdir(DIR *dir) {
if (tmp->dentry_data.data[0] == 0x00) // Not found
return 0;
print_disk_name(tmp->dentry_data.data);
#ifdef FS_DEBUG
kernel_printf("found!%d\n", 1);
kernel_printf("entry size %d\n", get_u32(tmp->dentry_data.data+28));
#endif
#ifdef FS_DEBUG
else {
kernel_printf("Not found h.txt\n");
}
#endif
}
}
// Get the next cluster
Expand All @@ -101,7 +87,7 @@ dirent *readdir(DIR *dir) {
#endif
crt_clu = get_next_clu_num(crt_clu);
#ifdef FS_DEBUG
kernel_printf("next clu:%d\n", next_clu);
kernel_printf("next clu:%d\n", crt_clu);
#endif
}
}
42 changes: 27 additions & 15 deletions kernel/mfs/fat32.c
Original file line number Diff line number Diff line change
Expand Up @@ -89,11 +89,11 @@ u32 load_root_dentries() {
pwd_dentry->name[0] = 0;
pwd_dentry->spinned = 1;
pwd_dentry->dentry_data.short_attr.attr = 0x10;
pwd_dentry->dentry_data.short_attr.starthi = 0;
pwd_dentry->dentry_data.short_attr.startlow = 2;
set_u16(pwd_dentry->dentry_data.data+20, 0);
set_u16(pwd_dentry->dentry_data.data+26, 2);
pwd_dentry->abs_sector_num = total_info.data_start_sector;
pwd_dentry->sector_dentry_offset = 0;

dcache_add(dcache, pwd_dentry);

struct mem_FATbuffer * result;
Expand All @@ -110,38 +110,48 @@ u32 load_root_dentries() {
}

u32 fat32_find(MY_FILE *file) {

u8 *path = file->path;
u8 disk_name_str[12];

int slash_traverser = 1;
int slash_traverser;
struct mem_dentry *crt_directory;
if (path[0] == '/') { // Wrong Path
if (path[0] == '/') {
if (path[1] == 0) {
file->disk_dentry_sector_num = root_dentry->abs_sector_num;
file->disk_dentry_num_offset = root_dentry->sector_dentry_offset;
return 0;
}
crt_directory = root_dentry;
slash_traverser = 1;

} else if (path[0] == 0) {
file->disk_dentry_sector_num = pwd_dentry->abs_sector_num;
file->disk_dentry_num_offset = pwd_dentry->sector_dentry_offset;
return 0;
} else {
crt_directory = pwd_dentry;
slash_traverser = 0;
}

u32 crt_clu;
u32 next_clu;
u32 slash_offset = fs_cut_slash(path+slash_traverser, disk_name_str);
disk_name_str[11] = 0;

// Traverse every file name in the path
while ( slash_offset != 0 && slash_offset != 0xFFFFFFFF ) {
if ( (crt_directory->dentry_data.short_attr.attr & 0x10) == 0 ) // Path isn't finished, but it is't sub directory
{
// kernel_printf("before return 1\n");
return 1;
}
// The address in FAT block
// Root -> 2
crt_clu = get_clu_by_dentry(crt_directory);

// kernel_printf("crt_clu = %d\n", crt_clu);

// Traverse every cluster of current direcroty
while (crt_clu != 0x0FFFFFFF) {

Expand Down Expand Up @@ -289,11 +299,6 @@ u32 fat32_write(MY_FILE *file, u8 *buf, u32 count) {

u32 crt_clus = get_start_clu_num(file);
u32 filesize = get_file_size(file);
kernel_printf("start clus got!\n");


kernel_printf("crt_clus : %d\n", crt_clus);
kernel_printf("file size is : %d\n", filesize);

// This file has no data before
if (crt_clus == 0) {
Expand All @@ -307,8 +312,6 @@ u32 fat32_write(MY_FILE *file, u8 *buf, u32 count) {
update_FAT(crt_clus, 0x0FFFFFFF);
}

if (file->crt_pointer_position + count > filesize)
count = filesize - file->crt_pointer_position;
u32 start_clus_num = file->crt_pointer_position / CLUSTER_SIZE;
u32 start_byte_num = file->crt_pointer_position % CLUSTER_SIZE;
u32 end_clus_num = (file->crt_pointer_position + count) / CLUSTER_SIZE;
Expand All @@ -334,15 +337,16 @@ u32 fat32_write(MY_FILE *file, u8 *buf, u32 count) {
if (clus_index >= start_clus_num && clus_index <= end_clus_num) {
// Get start and end byte num
if (clus_index == start_clus_num)
_start = start_clus_num;
_start = start_byte_num;
else
_start = 0;
if (clus_index == end_clus_num)
_end = end_clus_num;
_end = end_byte_num;
else
_end = CLUSTER_SIZE;

struct mem_page *crt_page = get_page(crt_clus-2);
// kernel_printf("_start = %d _end = %d\n", _start, _end);
kernel_memcpy(crt_page->p_data+_start, (void*)buf + buf_index, _end-_start);
crt_page->state = PAGE_DIRTY;
buf_index += _end - _start;
Expand All @@ -366,7 +370,15 @@ u32 fat32_write(MY_FILE *file, u8 *buf, u32 count) {
if (file->crt_pointer_position + count > filesize) {
u32 sec_num = file->disk_dentry_sector_num, offset = file->disk_dentry_num_offset;
u16 new_size = file->crt_pointer_position + count;
update_dentry(sec_num, offset, size, new_size);
// kernel_printf("newsize is %d\n", new_size);
// update_dentry(sec_num, offset, size, new_size);
struct mem_dentry * tmp_dentry = get_dentry(sec_num, offset);
u32 page_cluster_num = (sec_num - total_info.data_start_sector) / SEC_PER_CLU;
struct mem_page * tmp_page = get_page(page_cluster_num);
set_u32(tmp_dentry->dentry_data.data+28, new_size);
union disk_dentry *attributes = (union disk_dentry *)(tmp_page->p_data + offset * DENTRY_SIZE);
set_u32(attributes->data+28, new_size);
tmp_page->state = PAGE_DIRTY;
}

return buf_index;
Expand Down
2 changes: 2 additions & 0 deletions kernel/mfs/fat32cache.c
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
#include <zjunix/log.h>
#include <driver/vga.h>
#include <zjunix/slab.h>
#include <zjunix/utils.h>

Expand Down Expand Up @@ -59,6 +60,7 @@ struct mem_dentry * get_dentry(u32 sector_num, u32 offset) {
dcache_add(dcache, result);
return result;
} else {
kernel_printf("dcache look up found!\n");
return result;
}
}
Expand Down
9 changes: 1 addition & 8 deletions kernel/mfs/usr.c
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,6 @@ extern struct Total_FAT_Info total_info;
extern struct mem_dentry * pwd_dentry;

u32 fat32_cat(u8 *path) {
#ifdef FS_DEBUG
kernel_printf("In my cat function.\n");
#endif


u8 filename[12];
MY_FILE cat_file;
Expand All @@ -31,14 +27,11 @@ u32 fat32_cat(u8 *path) {
kernel_printf("The file size is %d\n", file_size);
#endif
u8 *buf = (u8 *)kmalloc(file_size + 1);
u8 *new_buf = (u8 *)kmalloc(2*file_size+1);

fat32_read(&cat_file, buf, file_size);
fat32_read(&cat_file, buf, file_size);
buf[file_size] = 0;
// fat32_write(&cat_file, buf, file_size);
//new_buf[2*file_size] = 0;
kernel_printf("%s\n", buf);
//kernel_printf("%s\n", new_buf);

fat32_close(&cat_file);
kfree(buf);
Expand Down
16 changes: 8 additions & 8 deletions kernel/mm/buddy.c
Original file line number Diff line number Diff line change
Expand Up @@ -135,22 +135,22 @@ struct page *__alloc_pages(unsigned int bplevel) {
unsigned int current_order, size;
struct page *page, *buddy_page;
struct freelist *free;
kernel_printf("enter __alloc_pages\n");
// kernel_printf("enter __alloc_pages\n");
lockup(&buddy.lock);
//search pages
for (current_order = bplevel; current_order <= MAX_BUDDY_ORDER; ++current_order) {
free = buddy.freelist + current_order;
kernel_printf("free == %x\n", free);
// kernel_printf("free == %x\n", free);
if (!list_empty(&(free->free_head)))
goto found;
}
//if not found
unlock(&buddy.lock);
kernel_printf("__alloc_pages not found\n");
// kernel_printf("__alloc_pages not found\n");
return 0;

found:
kernel_printf("have found\n");
// kernel_printf("have found\n");
page = container_of(free->free_head.next, struct page, list);
list_del_init(&(page->list));
set_bplevel(page, bplevel);
Expand All @@ -174,7 +174,7 @@ struct page *__alloc_pages(unsigned int bplevel) {
}

unlock(&buddy.lock);
kernel_printf("\n return page \n");
// kernel_printf("\n return page \n");
return page;
}

Expand All @@ -187,17 +187,17 @@ void *alloc_pages(unsigned int level) {
while(1<<bplevel<level)
bplevel++;

kernel_printf("bplevel == %x", bplevel);
// kernel_printf("bplevel == %x", bplevel);

struct page *page = __alloc_pages(bplevel);

if (!page)
{
kernel_printf("page ==0\n");
// kernel_printf("page ==0\n");
return 0;
}

kernel_printf("return (void *)((page - pages) << PAGE_SHIFT) \n");
// kernel_printf("return (void *)((page - pages) << PAGE_SHIFT) \n");
return (void *)((page - pages) << PAGE_SHIFT);
}

Expand Down
24 changes: 12 additions & 12 deletions kernel/mm/slab.c
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ void *slab_alloc(struct kmem_cache *cache) {
struct slab_head *s_head;
void *object = 0;
struct page *newpage;
kernel_printf("enter slab_alloc\n");
// kernel_printf("enter slab_alloc\n");
if (cache->cpu.page==0)
goto fullPage;
newpage = cache->cpu.page;
Expand All @@ -90,14 +90,14 @@ void *slab_alloc(struct kmem_cache *cache) {
kernel_printf("From Free-list\nnr_objs:%d\tobject:%x\tnew slabp:%x\n", s_head->nr_objs, object, newpage7s->slabp);
// kernel_getchar();
#endif // ! SLAB_DEBUG
kernel_printf("return FreeList-object\n");
// kernel_printf("return FreeList-object\n");
return object;
}

fullPage:
if(list_empty(&(cache->node.partial))){

kernel_printf("Page is Full\n");
// kernel_printf("Page is Full\n");

// call the buddy system to allocate one more page to be slab-cache
newpage = __alloc_pages(0);// get bplevel = 0 page === one page
Expand All @@ -118,7 +118,7 @@ void *slab_alloc(struct kmem_cache *cache) {
notFullPage:
if(s_head->isFull==0)
{
kernel_printf("enter notFullPage\n");
// kernel_printf("enter notFullPage\n");
object = s_head->end_ptr;
s_head->end_ptr = object + cache->size;
++(s_head->nr_objs);
Expand Down Expand Up @@ -157,7 +157,7 @@ void slab_free(struct kmem_cache *cache, void *object) {
struct slab_head *s_head = (struct slab_head *)KMEM_ADDR(opage, pages);

if (!(s_head->nr_objs)) {
kernel_printf("ERROR : slab_free error!\n");
// kernel_printf("ERROR : slab_free error!\n");
// die();
while (1) ;
}
Expand Down Expand Up @@ -201,7 +201,7 @@ unsigned int get_slab(unsigned int size) {
unsigned int bf_num = (1 << (PAGE_SHIFT - 1)); // half page
unsigned int bf_index = PAGE_SHIFT; // record the best fit num & index

kernel_printf("enter get_slab\n");
// kernel_printf("enter get_slab\n");
for (i = 0; i < itop; i++) {
if ((kmalloc_caches[i].objsize >= size) && (kmalloc_caches[i].objsize < bf_num)) {
bf_num = kmalloc_caches[i].objsize;
Expand All @@ -213,12 +213,12 @@ unsigned int get_slab(unsigned int size) {

void *kmalloc(unsigned int size) {
void *result;
kernel_printf("kmalloc size==%d\n", size);
// kernel_printf("kmalloc size==%d\n", size);
if (!size)
return 0;

result = phy_kmalloc(size);
kernel_printf("kmalloc reuslt==%x\n ", result);
// kernel_printf("kmalloc reuslt==%x\n ", result);
if (result)
return (void*)(KERNEL_ENTRY | (unsigned int)result);
else
Expand All @@ -228,7 +228,7 @@ void *kmalloc(unsigned int size) {
void *phy_kmalloc(unsigned int size) {
struct kmem_cache *cache;
unsigned int bf_index;
kernel_printf("enter phy_kmalloc\n");
// kernel_printf("enter phy_kmalloc\n");
if (!size)
return 0;

Expand All @@ -237,17 +237,17 @@ void *phy_kmalloc(unsigned int size) {
if (size > kmalloc_caches[PAGE_SHIFT - 1].objsize) {

size =Allign(size, 1<<PAGE_SHIFT);
kernel_printf("\n pyh_kmalloc size == %x\n", size);
// kernel_printf("\n pyh_kmalloc size == %x\n", size);
return alloc_pages(size >> PAGE_SHIFT);
}

bf_index = get_slab(size);
if (bf_index >= PAGE_SHIFT) {
kernel_printf("ERROR: No available slab\n");
// kernel_printf("ERROR: No available slab\n");
while (1)
;
}
kernel_printf("return slab_alloc\n");
// kernel_printf("return slab_alloc\n");
return slab_alloc(&(kmalloc_caches[bf_index]));
}

Expand Down
Loading

0 comments on commit 38638f7

Please sign in to comment.