xref: /minix3/minix/kernel/proc.h (revision 433d6423c39e34ec4b79c950597bb2d236f886be)
1 #ifndef PROC_H
2 #define PROC_H
3 
4 #include <minix/const.h>
5 #include <sys/cdefs.h>
6 
7 #ifndef __ASSEMBLY__
8 
9 /* Here is the declaration of the process table.  It contains all process
10  * data, including registers, flags, scheduling priority, memory map,
11  * accounting, message passing (IPC) information, and so on.
12  *
13  * Many assembly code routines reference fields in it.  The offsets to these
14  * fields are defined in the assembler include file sconst.h.  When changing
15  * struct proc, be sure to change sconst.h to match.
16  */
17 #include <minix/com.h>
18 #include <minix/portio.h>
19 #include "const.h"
20 #include "priv.h"
21 
22 struct proc {
23   struct stackframe_s p_reg;	/* process' registers saved in stack frame */
24   struct segframe p_seg;	/* segment descriptors */
25   proc_nr_t p_nr;		/* number of this process (for fast access) */
26   struct priv *p_priv;		/* system privileges structure */
27   volatile u32_t p_rts_flags;	/* process is runnable only if zero */
28   volatile u32_t p_misc_flags;	/* flags that do not suspend the process */
29 
30   char p_priority;		/* current process priority */
31   u64_t p_cpu_time_left;	/* time left to use the cpu */
32   unsigned p_quantum_size_ms;	/* assigned time quantum in ms
33 				   FIXME remove this */
34   struct proc *p_scheduler;	/* who should get out of quantum msg */
35   unsigned p_cpu;		/* what CPU is the process running on */
36 #ifdef CONFIG_SMP
37   bitchunk_t p_cpu_mask[BITMAP_CHUNKS(CONFIG_MAX_CPUS)]; /* what CPUs is the
38 							    process allowed to
39 							    run on */
40   bitchunk_t p_stale_tlb[BITMAP_CHUNKS(CONFIG_MAX_CPUS)]; /* On which cpu are
41 				possibly stale entries from this process and has
42 				to be fresed the next kernel touches this
43 				processes memory
44 				 */
45 #endif
46 
47   /* Accounting statistics that get passed to the process' scheduler */
48   struct {
49 	u64_t enter_queue;	/* time when enqueued (cycles) */
50 	u64_t time_in_queue;	/* time spent in queue */
51 	unsigned long dequeues;
52 	unsigned long ipc_sync;
53 	unsigned long ipc_async;
54 	unsigned long preempted;
55   } p_accounting;
56 
57   clock_t p_user_time;		/* user time in ticks */
58   clock_t p_sys_time;		/* sys time in ticks */
59 
60   clock_t p_virt_left;		/* number of ticks left on virtual timer */
61   clock_t p_prof_left;		/* number of ticks left on profile timer */
62 
63   u64_t p_cycles;		/* how many cycles did the process use */
64   u64_t p_kcall_cycles;		/* kernel cycles caused by this proc (kcall) */
65   u64_t p_kipc_cycles;		/* cycles caused by this proc (ipc) */
66 
67   struct proc *p_nextready;	/* pointer to next ready process */
68   struct proc *p_caller_q;	/* head of list of procs wishing to send */
69   struct proc *p_q_link;	/* link to next proc wishing to send */
70   endpoint_t p_getfrom_e;	/* from whom does process want to receive? */
71   endpoint_t p_sendto_e;	/* to whom does process want to send? */
72 
73   sigset_t p_pending;		/* bit map for pending kernel signals */
74 
75   char p_name[PROC_NAME_LEN];	/* name of the process, including \0 */
76 
77   endpoint_t p_endpoint;	/* endpoint number, generation-aware */
78 
79   message p_sendmsg;		/* Message from this process if SENDING */
80   message p_delivermsg;		/* Message for this process if MF_DELIVERMSG */
81   vir_bytes p_delivermsg_vir;	/* Virtual addr this proc wants message at */
82 
83   /* If handler functions detect a process wants to do something with
84    * memory that isn't present, VM has to fix it. Until it has asked
85    * what needs to be done and fixed it, save necessary state here.
86    *
87    * The requester gets a copy of its request message in reqmsg and gets
88    * VMREQUEST set.
89    */
90   struct {
91 	struct proc	*nextrestart;	/* next in vmrestart chain */
92 	struct proc	*nextrequestor;	/* next in vmrequest chain */
93 #define VMSTYPE_SYS_NONE	0
94 #define VMSTYPE_KERNELCALL	1
95 #define VMSTYPE_DELIVERMSG	2
96 #define VMSTYPE_MAP		3
97 
98 	int		type;		/* suspended operation */
99 	union {
100 		/* VMSTYPE_SYS_MESSAGE */
101 		message		reqmsg;	/* suspended request message */
102 	} saved;
103 
104 	/* Parameters of request to VM */
105 	int		req_type;
106 	endpoint_t	target;
107 	union {
108 		struct {
109 			vir_bytes 	start, length;	/* memory range */
110 			u8_t		writeflag;	/* nonzero for write access */
111 		} check;
112 	} params;
113 	/* VM result when available */
114 	int		vmresult;
115 
116 	/* If the suspended operation is a sys_call, its details are
117 	 * stored here.
118 	 */
119   } p_vmrequest;
120 
121   int p_found;	/* consistency checking variables */
122   int p_magic;		/* check validity of proc pointers */
123 
124   /* if MF_SC_DEFER is set, this struct is valid and contains the
125    * do_ipc() arguments that are still to be executed
126    */
127   struct { reg_t r1, r2, r3; } p_defer;
128 
129   u64_t p_signal_received;
130 
131 #if DEBUG_TRACE
132   int p_schedules;
133 #endif
134 };
135 
136 #endif /* __ASSEMBLY__ */
137 
138 /* Bits for the runtime flags. A process is runnable iff p_rts_flags == 0. */
139 #define RTS_SLOT_FREE	0x01	/* process slot is free */
140 #define RTS_PROC_STOP	0x02	/* process has been stopped */
141 #define RTS_SENDING	0x04	/* process blocked trying to send */
142 #define RTS_RECEIVING	0x08	/* process blocked trying to receive */
143 #define RTS_SIGNALED	0x10	/* set when new kernel signal arrives */
144 #define RTS_SIG_PENDING	0x20	/* unready while signal being processed */
145 #define RTS_P_STOP	0x40	/* set when process is being traced */
146 #define RTS_NO_PRIV	0x80	/* keep forked system process from running */
147 #define RTS_NO_ENDPOINT	0x100	/* process cannot send or receive messages */
148 #define RTS_VMINHIBIT	0x200	/* not scheduled until pagetable set by VM */
149 #define RTS_PAGEFAULT	0x400	/* process has unhandled pagefault */
150 #define RTS_VMREQUEST	0x800	/* originator of vm memory request */
151 #define RTS_VMREQTARGET	0x1000	/* target of vm memory request */
152 #define RTS_PREEMPTED	0x4000	/* this process was preempted by a higher
153 				   priority process and we should pick a new one
154 				   to run. Processes with this flag should be
155 				   returned to the front of their current
156 				   priority queue if they are still runnable
157 				   before we pick a new one
158 				 */
159 #define RTS_NO_QUANTUM	0x8000	/* process ran out of its quantum and we should
160 				   pick a new one. Process was dequeued and
161 				   should be enqueued at the end of some run
162 				   queue again */
163 #define RTS_BOOTINHIBIT	0x10000	/* not ready until VM has made it */
164 
165 /* A process is runnable iff p_rts_flags == 0. */
166 #define rts_f_is_runnable(flg)	((flg) == 0)
167 #define proc_is_runnable(p)	(rts_f_is_runnable((p)->p_rts_flags))
168 
169 #define proc_is_preempted(p)	((p)->p_rts_flags & RTS_PREEMPTED)
170 #define proc_no_quantum(p)	((p)->p_rts_flags & RTS_NO_QUANTUM)
171 #define proc_ptr_ok(p)		((p)->p_magic == PMAGIC)
172 #define proc_used_fpu(p)	((p)->p_misc_flags & (MF_FPU_INITIALIZED))
173 
174 /* test whether the process is scheduled by the kernel's default policy  */
175 #define proc_kernel_scheduler(p)	((p)->p_scheduler == NULL || \
176 					(p)->p_scheduler == (p))
177 
178 /* Macro to return: on which process is a certain process blocked?
179  * return endpoint number (can be ANY) or NONE. It's important to
180  * check RTS_SENDING first, and then RTS_RECEIVING, as they could
181  * both be on (if a ipc_sendrec() blocks on sending), and p_getfrom_e
182  * could be nonsense even though RTS_RECEIVING is on.
183  */
184 #define P_BLOCKEDON(p)							\
185 	(								\
186 		((p)->p_rts_flags & RTS_SENDING) ? 			\
187 		(p)->p_sendto_e : 					\
188 		(							\
189 			(						\
190 				((p)->p_rts_flags & RTS_RECEIVING) ?	\
191 				(p)->p_getfrom_e : 			\
192 				NONE					\
193 			) 						\
194 		)							\
195 	)
196 
197 /* These runtime flags can be tested and manipulated by these macros. */
198 
199 #define RTS_ISSET(rp, f) (((rp)->p_rts_flags & (f)) == (f))
200 
201 
202 /* Set flag and dequeue if the process was runnable. */
203 #define RTS_SET(rp, f)							\
204 	do {								\
205 		const int rts = (rp)->p_rts_flags;			\
206 		(rp)->p_rts_flags |= (f);				\
207 		if(rts_f_is_runnable(rts) && !proc_is_runnable(rp)) {	\
208 			dequeue(rp);					\
209 		}							\
210 	} while(0)
211 
212 /* Clear flag and enqueue if the process was not runnable but is now. */
213 #define RTS_UNSET(rp, f) 						\
214 	do {								\
215 		int rts;						\
216 		rts = (rp)->p_rts_flags;				\
217 		(rp)->p_rts_flags &= ~(f);				\
218 		if(!rts_f_is_runnable(rts) && proc_is_runnable(rp)) {	\
219 			enqueue(rp);					\
220 		}							\
221 	} while(0)
222 
223 /* Set flags to this value. */
224 #define RTS_SETFLAGS(rp, f)					\
225 	do {								\
226 		if(proc_is_runnable(rp) && (f)) { dequeue(rp); }		\
227 		(rp)->p_rts_flags = (f);				\
228 	} while(0)
229 
230 /* Misc flags */
231 #define MF_REPLY_PEND	0x001	/* reply to IPC_REQUEST is pending */
232 #define MF_VIRT_TIMER	0x002	/* process-virtual timer is running */
233 #define MF_PROF_TIMER	0x004	/* process-virtual profile timer is running */
234 #define MF_KCALL_RESUME 0x008	/* processing a kernel call was interrupted,
235 				   most likely because we need VM to resolve a
236 				   problem or a long running copy was preempted.
237 				   We need to resume the kernel call execution
238 				   now
239 				 */
240 #define MF_DELIVERMSG	0x040	/* Copy message for him before running */
241 #define MF_SIG_DELAY	0x080	/* Send signal when no longer sending */
242 #define MF_SC_ACTIVE	0x100	/* Syscall tracing: in a system call now */
243 #define MF_SC_DEFER	0x200	/* Syscall tracing: deferred system call */
244 #define MF_SC_TRACE	0x400	/* Syscall tracing: trigger syscall events */
245 #define MF_FPU_INITIALIZED	0x1000  /* process already used math, so fpu
246 					 * regs are significant (initialized)*/
247 #define MF_SENDING_FROM_KERNEL	0x2000 /* message of this process is from kernel */
248 #define MF_CONTEXT_SET	0x4000 /* don't touch context */
249 #define MF_SPROF_SEEN	0x8000 /* profiling has seen this process */
250 #define MF_FLUSH_TLB	0x10000	/* if set, TLB must be flushed before letting
251 				   this process run again. Currently it only
252 				   applies to SMP */
253 #define MF_SENDA_VM_MISS 0x20000 /* set if a processes wanted to receive an asyn
254 				    message from this sender but could not
255 				    because of VM modifying the sender's address
256 				    space*/
257 #define MF_STEP		 0x40000 /* Single-step process */
258 
259 /* Magic process table addresses. */
260 #define BEG_PROC_ADDR (&proc[0])
261 #define BEG_USER_ADDR (&proc[NR_TASKS])
262 #define END_PROC_ADDR (&proc[NR_TASKS + NR_PROCS])
263 
264 #define proc_addr(n)      (&(proc[NR_TASKS + (n)]))
265 #define proc_nr(p) 	  ((p)->p_nr)
266 
267 #define isokprocn(n)      ((unsigned) ((n) + NR_TASKS) < NR_PROCS + NR_TASKS)
268 #define isemptyn(n)       isemptyp(proc_addr(n))
269 #define isemptyp(p)       ((p)->p_rts_flags == RTS_SLOT_FREE)
270 #define iskernelp(p)	  ((p) < BEG_USER_ADDR)
271 #define iskerneln(n)	  ((n) < 0)
272 #define isuserp(p)        isusern((p) >= BEG_USER_ADDR)
273 #define isusern(n)        ((n) >= 0)
274 #define isrootsysn(n)	  ((n) == ROOT_SYS_PROC_NR)
275 
276 #ifndef __ASSEMBLY__
277 
278 EXTERN struct proc proc[NR_TASKS + NR_PROCS];	/* process table */
279 
280 int mini_send(struct proc *caller_ptr, endpoint_t dst_e, message *m_ptr,
281 	int flags);
282 
283 #endif /* __ASSEMBLY__ */
284 
285 #endif /* PROC_H */
286