17 #include <arch/arch.h>
19 volatile int magic_mem[10];
22 frontend_proc_init(struct proc *SAFE p)
24 #ifdef CONFIG_APPSERVER
25 pid_t parent_id = p->ppid, id = p->pid;
27 if(frontend_syscall(parent_id,APPSERVER_SYSCALL_proc_init,id,0,0,0,&errno))
28 panic("Front-end server couldn't initialize new process!");
33 frontend_proc_free(struct proc *SAFE p)
35 #ifdef CONFIG_APPSERVER
37 if(frontend_syscall(0,APPSERVER_SYSCALL_proc_free,p->pid,0,0,0,&errno))
38 panic("Front-end server couldn't free process!");
42 struct kmem_cache* struct_file_cache;
45 struct_file_cache = kmem_cache_create("struct_file",
46 sizeof(struct file), 8, 0, 0, 0);
49 /* will zero anything in the page after the EOF */
50 error_t file_read_page(struct file* f, physaddr_t pa, size_t pgoff)
52 int ret = frontend_syscall(0,APPSERVER_SYSCALL_pread,f->fd,pa,PGSIZE,
55 memset(KADDR(pa)+ret,0,PGSIZE-ret);
59 struct file* file_open_from_fd(struct proc* p, int fd)
61 struct file* f = NULL;
62 if(!(f = kmem_cache_alloc(struct_file_cache,0)))
65 f->fd = frontend_syscall(p->pid,APPSERVER_SYSCALL_kdup,fd,0,0,0,NULL);
68 kmem_cache_free(struct_file_cache,f);
72 spinlock_init(&f->lock);
79 struct file* file_open(const char* path, int oflag, int mode)
81 struct file* f = NULL;
82 // although path is a kernel pointer, it may be below KERNBASE.
84 char* malloced = NULL;
85 if((uintptr_t)path < KERNBASE)
87 size_t len = strlen(path)+1;
88 malloced = kmalloc(len,0);
91 path = memcpy(malloced,path,len);
94 if(!(f = kmem_cache_alloc(struct_file_cache,0)))
97 f->fd = frontend_syscall(0,APPSERVER_SYSCALL_open,PADDR(path),
101 kmem_cache_free(struct_file_cache,f);
105 spinlock_init(&f->lock);
114 void file_incref(struct file* f)
118 spin_unlock(&f->lock);
121 void file_decref(struct file* f)
123 // if you decref too many times, you'll clobber memory :(
127 int ret = frontend_syscall(0,APPSERVER_SYSCALL_close,f->fd,0,0,0,NULL);
129 kmem_cache_free(struct_file_cache,f);
132 spin_unlock(&f->lock);
135 int frontend_syscall_errno(struct proc* p, int n, int a0, int a1, int a2, int a3)
137 int errno, ret = frontend_syscall(p->pid,n,a0,a1,a2,a3,&errno);
143 int32_t frontend_syscall(pid_t pid, int32_t syscall_num,
144 uint32_t arg0, uint32_t arg1,
145 uint32_t arg2, uint32_t arg3, int32_t* errno)
147 #ifndef CONFIG_APPSERVER
148 warn("No appserver support, requested syscall %d for proc %d", syscall_num,
156 if (!irq_is_enabled())
157 warn("IRQ is disabled in frontend_syscall %d for proc %d\n", syscall_num, pid);
160 static spinlock_t lock = SPINLOCK_INITIALIZER;
163 // only one frontend request at a time.
164 // interrupts could try to do frontend requests,
165 // which would deadlock, so disable them
168 // write syscall into magic memory
170 magic_mem[1] = syscall_num;
178 // wait for front-end response
179 while(magic_mem[7] == 0)
184 *errno = magic_mem[2];
191 void __diediedie(uint32_t srcid, uint32_t code, uint32_t a1, uint32_t a2)
194 frontend_syscall(0,APPSERVER_SYSCALL_exit,(int)code,0,0,0,&errno);
198 void appserver_die(uintptr_t code)
201 for(i = 0; i < num_cpus; i++)
203 while(send_kernel_message(i, (amr_t)&__diediedie, code, 0, 0,
207 __diediedie(0, code, 0, 0);