📄 fs.c
字号:
/*
* Copyright 1999, 2000, 2001, 2002 Lucent Technologies Inc.
* All Rights Reserved.
* Information Sciences Research Center, Bell Labs.
*
* LUCENT TECHNOLOGIES DOES NOT CLAIM MERCHANTABILITY OF THIS SOFTWARE
* OR THE SUITABILITY OF THIS SOFTWARE FOR ANY PARTICULAR PURPOSE. The
* software is provided "as is" without expressed or implied warranty
* of any kind.
*
* These notices must be retained in any copies of any part of this
* software.
*
*/
/*
* A user-level file system mock-up.
*/
#include "string.h"
#include "pebble.h"
#include "synch.h"
#define N 1024
#define SHM_SIZE (1024*1024)
static void *page[N];
const char PORTAL_CREATE[] = "too many open portals";
int
fs_write1(Thread *t, char *buf, int n)
{
printf("fs_write: buf=%p n=%d asid=%d\n", buf, n, get_asid());
printf("touching buf[0]=%d\n", (uchar)buf[0]);
return n;
}
int
fs_read1(Thread *t, char *buf, int n)
{
printf("fs_read: buf=%p n=%d\n", buf, n);
buf[0]++;
printf("new buf[0]=%d\n", (uchar)buf[0]);
return n;
}
int
fs_write2(Thread *t, char *buf, int n)
{
return n;
}
int
fs_read2(Thread *t, char *buf, int n)
{
buf[0]++;
return n;
}
int
fs_give(Thread *t, void *p, int i)
{
if (i >= 0 && i < N)
page[i] = p;
return 0;
}
int
fs_peek(Thread *t, int *p)
{
return *p;
}
int
fs_take(int caller_asid, int i)
{
int code = -1;
if (i >= 0 && i < N)
code = vm_give_page(caller_asid, page[i]);
if (code < 0)
return -1;
else
return (int)page[i];
}
int
fs_swap(Thread *t, void *p, int i)
{
int code = -1;
if (i >= 0 && i < N)
code = vm_swap_page(p, page[i]);
return code;
}
int
fs_try(Thread *t, Lock *lock)
{
return mutex_trylock(lock);
}
int
fs_share(Thread *t, void *p)
{
return 0;
}
int
fs_set(Thread *t, int *p, int val)
{
*p = val;
return 0;
}
int
fs_unmap(Thread *t, void *p)
{
return vm_unmap_page(p);
}
int
fs_count(Thread *t, void *p)
{
return vm_count_page(p);
}
/* main file system thread */
int main()
{
int x; /* debug */
int s, sz;
char *addr;
int give_id, window_peek_id, peek_id, take_id, swap_id, try_id;
int set_id, share_id, unmap_id, count_id;
printf("fs is active. asid=%d stack at %p\n", get_asid(), &x);
/* verify that we are running in user mode with interrupts enabled */
if (!check_psw(1,1)) {
printf("fs: invalid processor status: %08lx\n", get_psw());
task_exit(1);
}
if ((s = sem_create(1)) < 0)
panic("sem_create:");
if (sem_name(s, "fs_sem") < 0)
panic("sem_name(fs_sem):");
if (portal_create_pair_template("fs1", fs_read1, fs_write1, "smtwi", 0)
< 0)
panic("portal_create_pair:");
if (portal_create_pair_template("fs2", fs_read2, fs_write2, "smtwi", 0)
< 0)
panic("portal_create_pair:");
if ((give_id = portal_create(-1, "smt>ii", 0, fs_give, 0)) < 0)
panic("portal_create of fs_give:");
if (portal_name(give_id, "fs_give", 1) < 0)
panic("portal_name of fs_give:");
if ((peek_id = portal_create(-1, "smtiii", 0, fs_peek, 0)) < 0)
panic("portal_create of fs_peek:");
if (portal_name(peek_id, "fs_peek", 1) < 0)
panic("portal_name of fs_peek:");
if ((window_peek_id = portal_create(-1, "smtwii", 0, fs_peek, 0)) < 0)
panic("portal_create of fs_window_peek:");
if (portal_name(window_peek_id, "fs_window_peek", 1) < 0)
panic("portal_name of fs_peek:");
if ((take_id = portal_create(-1, "smaiii", 0, fs_take, 0)) < 0)
panic("portal_create of fs_take:");
if (portal_name(take_id, "fs_take", 1) < 0)
panic("portal_name of fs_take:");
if ((swap_id = portal_create(-1, "smtwii", 0, fs_swap, 0)) < 0)
panic("portal_create of fs_swap:");
if (portal_name(swap_id, "fs_swap", 1) < 0)
panic("portal_name of fs_swap:");
if ((try_id = portal_create(-1, "smtiii", 0, fs_try, 0)) < 0)
panic("portal_create of fs_try:");
if (portal_name(try_id, "fs_try", 1) < 0)
panic("portal_name of fs_try:");
if ((share_id = portal_create(-1, "smt=ii", 0, fs_share, 0)) < 0)
panic("portal_create of fs_share:");
if (portal_name(share_id, "fs_share", 1) < 0)
panic("portal_name of fs_share:");
if ((set_id = portal_create(-1, "smtiii", 0, fs_set, 0)) < 0)
panic("portal_create of fs_set:");
if (portal_name(set_id, "fs_set", 1) < 0)
panic("portal_name of fs_set:");
if ((unmap_id = portal_create(-1, "smtiii", 0, fs_unmap, 0)) < 0)
panic("portal_create of fs_unmap:");
if (portal_name(unmap_id, "fs_unmap", 1) < 0)
panic("portal_name of fs_unmap:");
if ((count_id = portal_create(-1, "smtiii", 0, fs_count, 0)) < 0)
panic("portal_create of fs_count:");
if (portal_name(count_id, "fs_count", 1) < 0)
panic("portal_name of fs_count:");
if ((s = sem_create(0)) < 0)
panic("sem_create");
if ((addr = shm_create(SHM_SIZE)) == (char *)-1)
panic("shm_create:");
if (shm_name(addr, "shared") < 0)
panic("shm_name:");
if ((sz = shm_size(addr)) != SHM_SIZE) {
printf("shm_size returned %d; should be %d\n", sz, SHM_SIZE);
perror("shm_size");
task_exit(1);
}
printf("shared area start=%p size=%d\n", addr, sz);
printf("fs waiting forever...\n");
/* wait forever */
sem_wait(s);
/*
* return to initialization code.
* cannot just "return", since the startup code (crt0.S) calls
* exit when main routine terminates.
*/
call_portal(SYS_RTN_RPC);
return(1);
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -