Add Linux's math64.h
[akaros.git] / kern / src / arsc.c
1 /* See COPYRIGHT for copyright information. */
2
3 #include <ros/common.h>
4 #include <ros/ring_syscall.h>
5 #include <arch/types.h>
6 #include <arch/arch.h>
7 #include <arch/mmu.h>
8 #include <error.h>
9
10 #include <syscall.h>
11 #include <kmalloc.h>
12 #include <pmap.h>
13 #include <stdio.h>
14 #include <hashtable.h>
15 #include <smp.h>
16 #include <arsc_server.h>
17 #include <kref.h>
18
19
20
21 struct proc_list arsc_proc_list = TAILQ_HEAD_INITIALIZER(arsc_proc_list);
22 spinlock_t arsc_proc_lock = SPINLOCK_INITIALIZER_IRQSAVE;
23
24 intreg_t inline syscall_async(struct proc *p, syscall_req_t *call)
25 {
26         struct syscall* sc = call->sc;
27         return syscall(p, sc->num, sc->arg0, sc->arg1,
28                        sc->arg2, sc->arg3, sc->arg4, sc->arg5);
29 }
30
31 syscall_sring_t* sys_init_arsc(struct proc *p)
32 {
33         kref_get(&p->p_kref, 1);                /* we're storing an external ref here */
34         syscall_sring_t* sring;
35         void * va;
36         // TODO: need to pin this page in the future when swapping happens
37         va = do_mmap(p,MMAP_LOWEST_VA, SYSCALLRINGSIZE, PROT_READ | PROT_WRITE,
38                      MAP_ANONYMOUS | MAP_POPULATE | MAP_PRIVATE, NULL, 0);
39         pte_t pte = pgdir_walk(p->env_pgdir, (void*)va, 0);
40         assert(pte_walk_okay(pte));
41         sring = (syscall_sring_t*) KADDR(pte_get_paddr(pte));
42         /*make sure we are able to allocate the shared ring */
43         assert(sring != NULL);
44         p->procdata->syscallring = sring;
45         /* Initialize the generic syscall ring buffer */
46         SHARED_RING_INIT(sring);
47
48         BACK_RING_INIT(&p->syscallbackring,
49                        sring,
50                        SYSCALLRINGSIZE);
51
52         spin_lock_irqsave(&arsc_proc_lock);
53         TAILQ_INSERT_TAIL(&arsc_proc_list, p, proc_arsc_link);
54         spin_unlock_irqsave(&arsc_proc_lock);
55         return (syscall_sring_t*)va;
56 }
57
58 void arsc_server(uint32_t srcid, long a0, long a1, long a2)
59 {
60         struct proc *p = NULL;
61         TAILQ_INIT(&arsc_proc_list);
62         while (1) {
63                 while (TAILQ_EMPTY(&arsc_proc_list))
64                         cpu_relax();
65
66                 TAILQ_FOREACH(p, &arsc_proc_list, proc_arsc_link) {
67                         /* Probably want to try to process a dying process's syscalls.  If
68                          * not, just move it to an else case */
69                         process_generic_syscalls (p, MAX_ASRC_BATCH);
70                         if (proc_is_dying(p)) {
71                                 TAILQ_REMOVE(&arsc_proc_list, p, proc_arsc_link);
72                                 proc_decref(p);
73                                 /* Need to break out, so the TAILQ_FOREACH doesn't flip out.
74                                  * It's not fair, but we're not dealing with that yet anyway */
75                                 break;
76                         }
77                 }
78         }
79 }
80
81 static intreg_t process_generic_syscalls(struct proc *p, size_t max)
82 {
83         size_t count = 0;
84         syscall_back_ring_t* sysbr = &p->syscallbackring;
85         struct per_cpu_info* pcpui = &per_cpu_info[core_id()];
86         uintptr_t old_proc;
87         // looking at a process not initialized to perform arsc.
88         if (sysbr == NULL)
89                 return count;
90         /* Bail out if there is nothing to do */
91         if (!RING_HAS_UNCONSUMED_REQUESTS(sysbr))
92                 return 0;
93         /* Switch to the address space of the process, so we can handle their
94          * pointers, etc. */
95         old_proc = switch_to(p);
96         // max is the most we'll process.  max = 0 means do as many as possible
97         // TODO: check for initialization of the ring.
98         while (RING_HAS_UNCONSUMED_REQUESTS(sysbr) && ((!max)||(count < max)) ) {
99                 // ASSUME: one queue per process
100                 count++;
101                 //printk("DEBUG PRE: sring->req_prod: %d, sring->rsp_prod: %d\n",
102                 //         sysbr->sring->req_prod, sysbr->sring->rsp_prod);
103                 // might want to think about 0-ing this out, if we aren't
104                 // going to explicitly fill in all fields
105                 syscall_rsp_t rsp;
106                 // this assumes we get our answer immediately for the syscall.
107                 syscall_req_t* req = RING_GET_REQUEST(sysbr, ++sysbr->req_cons);
108
109                 pcpui->cur_kthread->sysc = req->sc;
110                 run_local_syscall(req->sc); // TODO: blocking call will block arcs as well.
111
112                 // need to keep the slot in the ring buffer if it is blocked
113                 (sysbr->rsp_prod_pvt)++;
114                 req->status = RES_ready;
115                 RING_PUSH_RESPONSES(sysbr);
116
117                 //printk("DEBUG POST: sring->req_prod: %d, sring->rsp_prod: %d\n",
118                 //         sysbr->sring->req_prod, sysbr->sring->rsp_prod);
119         }
120         /* switch back to whatever context we were in before */
121         switch_back(p, old_proc);
122         return (intreg_t)count;
123 }
124