Fix extra decref of shared_page
[akaros.git] / kern / src / frontend.c
1 #include <atomic.h>
2 #include <process.h>
3 #include <kmalloc.h>
4 #include <pmap.h>
5 #include <frontend.h>
6 #include <syscall.h>
7 #include <smp.h>
8 #include <slab.h>
9 #include <arch/arch.h>
10
11 volatile int magic_mem[10];
12
13 void
14 frontend_proc_init(struct proc *p)
15 {
16 #ifdef CONFIG_APPSERVER
17         pid_t parent_id = p->ppid, id = p->pid;
18         int32_t errno;
19         if(frontend_syscall(parent_id,APPSERVER_SYSCALL_proc_init,id,0,0,0,&errno))
20                 panic("Front-end server couldn't initialize new process!");
21 #endif
22 }
23
24 void
25 frontend_proc_free(struct proc *p)
26 {
27 #ifdef CONFIG_APPSERVER
28         int32_t errno;
29         if(frontend_syscall(0,APPSERVER_SYSCALL_proc_free,p->pid,0,0,0,&errno))
30                 panic("Front-end server couldn't free process!");
31 #endif
32 }
33
34 struct kmem_cache* struct_file_cache;
35 void file_init()
36 {
37         struct_file_cache = kmem_cache_create("struct_file",
38                                               sizeof(struct file), 8, 0, 0, 0);
39 }
40
41 /* will zero anything in the page after the EOF */
42 error_t file_read_page(struct file* f, physaddr_t pa, size_t pgoff)
43 {
44         int ret = frontend_syscall(0,APPSERVER_SYSCALL_pread,f->fd,pa,PGSIZE,
45                                    pgoff*PGSIZE,NULL);
46         if(ret >= 0)
47                 memset(KADDR(pa)+ret,0,PGSIZE-ret);
48         return ret;
49 }
50
51 struct file* file_open_from_fd(struct proc* p, int fd)
52 {
53         struct file* f = NULL;
54         if(!(f = kmem_cache_alloc(struct_file_cache,0)))
55                 goto out;
56
57         f->fd = frontend_syscall(p->pid,APPSERVER_SYSCALL_kdup,fd,0,0,0,NULL);
58         if(f->fd == -1)
59         {
60                 kmem_cache_free(struct_file_cache,f);
61                 f = NULL;
62                 goto out;
63         }
64         spinlock_init(&f->lock);
65         f->refcnt = 1;
66
67 out:
68         return f;
69 }
70
71 struct file* file_open(const char* path, int oflag, int mode)
72 {
73         struct file* f = NULL;
74         // although path is a kernel pointer, it may be below KERNBASE.
75         // fix that if so.
76         char* malloced = NULL;
77         if((uintptr_t)path < KERNBASE)
78         {
79                 size_t len = strlen(path)+1;
80                 malloced = kmalloc(len,0);
81                 if(!malloced)
82                         goto out;
83                 path = memcpy(malloced,path,len);
84         }
85
86         if(!(f = kmem_cache_alloc(struct_file_cache,0)))
87                 goto out;
88
89         f->fd = frontend_syscall(0,APPSERVER_SYSCALL_open,PADDR(path),
90                                  oflag,mode,0,NULL);
91         if(f->fd == -1)
92         {
93                 kmem_cache_free(struct_file_cache,f);
94                 f = NULL;
95                 goto out;
96         }
97         spinlock_init(&f->lock);
98         f->refcnt = 1;
99
100 out:
101         if(malloced)
102                 kfree(malloced);
103         return f;
104 }
105
106 void file_incref(struct file* f)
107 {
108         spin_lock(&f->lock);
109         f->refcnt++;
110         spin_unlock(&f->lock);
111 }
112
113 void file_decref(struct file* f)
114 {
115         // if you decref too many times, you'll clobber memory :(
116         spin_lock(&f->lock);
117         if(--f->refcnt == 0)
118         {
119                 int ret = frontend_syscall(0,APPSERVER_SYSCALL_close,f->fd,0,0,0,NULL);
120                 assert(ret == 0);
121                 kmem_cache_free(struct_file_cache,f);
122         }
123         else
124                 spin_unlock(&f->lock);
125 }
126
127 int frontend_syscall_errno(struct proc* p, int n, int a0, int a1, int a2, int a3)
128 {
129         int errno, ret = frontend_syscall(p->pid,n,a0,a1,a2,a3,&errno);
130         if(errno && p)
131                 set_errno(errno);
132         return ret;
133 }
134
135 int32_t frontend_syscall(pid_t pid, int32_t syscall_num,
136                          uint32_t arg0, uint32_t arg1,
137                          uint32_t arg2, uint32_t arg3, int32_t* errno)
138 {
139 #ifndef CONFIG_APPSERVER
140         warn("No appserver support, requested syscall %d for proc %d", syscall_num,
141              pid);
142         if(errno)
143                 *errno = ENOSYS;
144         return -1;
145 #endif
146
147 #ifdef CONFIG_X86
148         if (!irq_is_enabled())
149                 warn("IRQ is disabled in frontend_syscall %d for proc %d\n", syscall_num, pid);
150 #endif
151
152         static spinlock_t lock = SPINLOCK_INITIALIZER;
153         int32_t ret;
154
155         // only one frontend request at a time.
156         // interrupts could try to do frontend requests,
157         // which would deadlock, so disable them
158         spin_lock(&lock);
159
160         // write syscall into magic memory
161         magic_mem[7] = 0;
162         magic_mem[1] = syscall_num;
163         magic_mem[2] = arg0;
164         magic_mem[3] = arg1;
165         magic_mem[4] = arg2;
166         magic_mem[5] = arg3;
167         magic_mem[6] = pid;
168         magic_mem[0] = 0x80;
169
170         // wait for front-end response
171         while(magic_mem[7] == 0)
172                 ;
173
174         ret = magic_mem[1];
175         if(errno)
176                 *errno = magic_mem[2];
177
178         spin_unlock(&lock);
179
180         return ret;
181 }
182
183 void __diediedie(uint32_t srcid, uint32_t code, uint32_t a1, uint32_t a2)
184 {
185         int32_t errno;
186         frontend_syscall(0,APPSERVER_SYSCALL_exit,(int)code,0,0,0,&errno);
187         while(1);
188 }
189
190 void appserver_die(uintptr_t code)
191 {
192         int i;
193         for(i = 0; i < num_cores; i++)
194                 if(i != core_id())
195                         while(send_kernel_message(i, (amr_t)&__diediedie, code, 0, 0,
196                                                   KMSG_IMMEDIATE));
197
198         // just in case.
199         __diediedie(0, code, 0, 0);
200 }