Add proc_global_info (XCC)
[akaros.git] / kern / include / ros / procinfo.h
1 /* See COPYRIGHT for copyright information. */
2
3 #pragma once
4
5 #include <ros/memlayout.h>
6 #include <ros/common.h>
7 #include <ros/resource.h>
8 #include <ros/atomic.h>
9 #include <ros/arch/arch.h>
10 #include <string.h>
11
12 /* Process creation flags */
13 #define PROC_DUP_FGRP                   1
14
15 #define PROCINFO_MAX_ARGP 32
16 #define PROCINFO_ARGBUF_SIZE 3072
17
18 #ifdef ROS_KERNEL
19 #include <sys/queue.h>
20 #endif /* ROS_KERNEL */
21
22 /* Not necessary to expose all of this, but it doesn't hurt, and is convenient
23  * for the kernel.  Need to do some acrobatics for the TAILQ_ENTRY. */
24 struct vcore;
25 struct vcore {
26 #ifdef ROS_KERNEL
27         TAILQ_ENTRY(vcore)      list;
28 #else /* userspace */
29         void                            *dummy_ptr1;
30         void                            *dummy_ptr2;
31 #endif /* ROS_KERNEL */
32         uint32_t                        pcoreid;
33         bool                            valid;
34         uint32_t                        nr_preempts_sent;       /* these two differ when a preempt*/
35         uint32_t                        nr_preempts_done;       /* is in flight. */
36         uint64_t                        preempt_pending;
37         /* A process can see cumulative runtime as of the last resume, and can also
38          * calculate runtime in this interval, by adding (ns - resume) + total. */
39         uint64_t                        resume_ticks;           /* TSC at resume time */
40         uint64_t                        total_ticks;            /* ticks up to last offlining */
41 };
42
43 struct pcore {
44         uint32_t                        vcoreid;
45         bool                            valid;
46 };
47
48 typedef struct procinfo {
49         pid_t pid;
50         pid_t ppid;
51         size_t max_vcores;      /* TODO: change to a uint32_t */
52         uint64_t tsc_freq;
53         uint64_t timing_overhead;
54         void *heap_bottom;
55         /* glibc relies on stuff above this point.  if you change it, you need to
56          * rebuild glibc. */
57         bool is_mcp;                    /* is in multi mode */
58         unsigned long           res_grant[MAX_NUM_RESOURCES];
59         struct vcore            vcoremap[MAX_NUM_CORES];
60         uint32_t                        num_vcores;
61         struct pcore            pcoremap[MAX_NUM_CORES];
62         seq_ctr_t                       coremap_seqctr;
63 } procinfo_t;
64 #define PROCINFO_NUM_PAGES  ((sizeof(procinfo_t)-1)/PGSIZE + 1)
65
66 /* We align this so that the kernel can easily allocate it in the BSS */
67 struct proc_global_info {
68
69 } __attribute__((aligned(PGSIZE)));
70 #define PROCGINFO_NUM_PAGES  (sizeof(struct proc_global_info) / PGSIZE)
71
72 #ifdef ROS_KERNEL
73
74 /* defined in init.c */
75 extern struct proc_global_info __proc_global_info;
76
77 #else /* Userland */
78
79 #define __procinfo (*(procinfo_t*)UINFO)
80 #define __proc_global_info (*(struct proc_global_info*)UGINFO)
81
82 #include <ros/common.h>
83 #include <ros/atomic.h>
84 #include <ros/syscall.h>
85
86 /* Figure out what your vcoreid is from your pcoreid and procinfo.  Only low
87  * level or debugging code should call this. */
88 static inline uint32_t __get_vcoreid_from_procinfo(void)
89 {
90         /* The assumption is that any IPIs/KMSGs would knock userspace into the
91          * kernel before it could read the closing of the seqctr.  Put another way,
92          * there is a 'memory barrier' between the IPI write and the seqctr write.
93          * I think this is true. */
94         uint32_t kpcoreid, kvcoreid;
95         seq_ctr_t old_seq;
96         do {
97                 cmb();
98                 old_seq = __procinfo.coremap_seqctr;
99                 kpcoreid = __ros_syscall_noerrno(SYS_getpcoreid, 0, 0, 0, 0, 0, 0);
100                 if (!__procinfo.pcoremap[kpcoreid].valid)
101                         continue;
102                 kvcoreid = __procinfo.pcoremap[kpcoreid].vcoreid;
103         } while (seqctr_retry(old_seq, __procinfo.coremap_seqctr));
104         return kvcoreid;
105 }
106
107 static inline uint32_t __get_vcoreid(void)
108 {
109         /* since sys_getvcoreid could lie (and might never change) */
110         return __get_vcoreid_from_procinfo();
111 }
112
113 #endif /* ifndef ROS_KERNEL */