1 /* This file contains some utility routines for RS. 2 * 3 * Changes: 4 * Nov 22, 2009: Created (Cristiano Giuffrida) 5 */ 6 7 #include "inc.h" 8 9 #include <assert.h> 10 #include <minix/sched.h> 11 #include "kernel/proc.h" 12 13 /*===========================================================================* 14 * init_service * 15 *===========================================================================*/ 16 int init_service(rp, type) 17 struct rproc *rp; /* pointer to process slot */ 18 int type; /* type of initialization */ 19 { 20 int r; 21 message m; 22 struct rprocpub *rpub; 23 endpoint_t old_endpoint; 24 25 rpub = rp->r_pub; 26 27 rp->r_flags |= RS_INITIALIZING; /* now initializing */ 28 rp->r_check_tm = rp->r_alive_tm + 1; /* expect reply within period */ 29 30 /* In case of RS initialization, we are done. */ 31 if(rp->r_priv.s_flags & ROOT_SYS_PROC) { 32 return OK; 33 } 34 35 /* Determine the old endpoint if this is a new instance. */ 36 old_endpoint = NONE; 37 if(rp->r_old_rp) { 38 old_endpoint = rp->r_old_rp->r_pub->endpoint; 39 } 40 else if(rp->r_prev_rp) { 41 old_endpoint = rp->r_prev_rp->r_pub->endpoint; 42 } 43 44 /* Send initialization message. */ 45 m.m_type = RS_INIT; 46 m.m_rs_init.type = type; 47 m.m_rs_init.rproctab_gid = rinit.rproctab_gid; 48 m.m_rs_init.old_endpoint = old_endpoint; 49 r = asynsend(rpub->endpoint, &m); 50 51 return r; 52 } 53 54 /*===========================================================================* 55 * fill_send_mask * 56 *===========================================================================*/ 57 void fill_send_mask(send_mask, set_bits) 58 sys_map_t *send_mask; /* the send mask to fill in */ 59 int set_bits; /* TRUE sets all bits, FALSE clears all bits */ 60 { 61 /* Fill in a send mask. */ 62 int i; 63 64 for (i = 0; i < NR_SYS_PROCS; i++) { 65 if (set_bits) 66 set_sys_bit(*send_mask, i); 67 else 68 unset_sys_bit(*send_mask, i); 69 } 70 } 71 72 /*===========================================================================* 73 * fill_call_mask * 74 *===========================================================================*/ 75 void fill_call_mask(calls, tot_nr_calls, call_mask, call_base, is_init) 76 int *calls; /* the unordered set of calls */ 77 int tot_nr_calls; /* the total number of calls */ 78 bitchunk_t *call_mask; /* the call mask to fill in */ 79 int call_base; /* the base offset for the calls */ 80 int is_init; /* set when initializing a call mask */ 81 { 82 /* Fill a call mask from an unordered set of calls. */ 83 int i; 84 int call_mask_size, nr_calls; 85 86 call_mask_size = BITMAP_CHUNKS(tot_nr_calls); 87 88 /* Count the number of calls to fill in. */ 89 nr_calls = 0; 90 for(i=0; calls[i] != NULL_C; i++) { 91 nr_calls++; 92 } 93 94 /* See if all calls are allowed and call mask must be completely filled. */ 95 if(nr_calls == 1 && calls[0] == ALL_C) { 96 for(i=0; i < call_mask_size; i++) { 97 call_mask[i] = (~0); 98 } 99 } 100 else { 101 /* When initializing, reset the mask first. */ 102 if(is_init) { 103 for(i=0; i < call_mask_size; i++) { 104 call_mask[i] = 0; 105 } 106 } 107 /* Enter calls bit by bit. */ 108 for(i=0; i < nr_calls; i++) { 109 SET_BIT(call_mask, calls[i] - call_base); 110 } 111 } 112 } 113 114 /*===========================================================================* 115 * srv_to_string * 116 *===========================================================================*/ 117 char* srv_to_string(rp) 118 struct rproc *rp; /* pointer to process slot */ 119 { 120 struct rprocpub *rpub; 121 int slot_nr; 122 char *srv_string; 123 static char srv_string_pool[3][RS_MAX_LABEL_LEN + (DEBUG ? 256 : 64)]; 124 static int srv_string_pool_index = 0; 125 126 rpub = rp->r_pub; 127 slot_nr = rp - rproc; 128 srv_string = srv_string_pool[srv_string_pool_index]; 129 srv_string_pool_index = (srv_string_pool_index + 1) % 3; 130 131 #define srv_str(cmd) ((cmd) == NULL || (cmd)[0] == '\0' ? "_" : (cmd)) 132 #define srv_ep_str(rp) (itoa((rp)->r_pub->endpoint)) 133 #define srv_active_str(rp) ((rp)->r_flags & RS_ACTIVE ? "*" : " ") 134 #define srv_version_str(rp) ((rp)->r_new_rp || (rp)->r_next_rp ? "-" : \ 135 ((rp)->r_old_rp || (rp)->r_prev_rp ? "+" : " ")) 136 137 #if DEBUG 138 sprintf(srv_string, "service '%s'%s%s(slot %d, ep %d, pid %d, cmd %s, script %s, proc %s, major %d, flags 0x%03x, sys_flags 0x%02x)", 139 rpub->label, srv_active_str(rp), srv_version_str(rp), 140 slot_nr, rpub->endpoint, rp->r_pid, srv_str(rp->r_cmd), 141 srv_str(rp->r_script), srv_str(rpub->proc_name), rpub->dev_nr, 142 rp->r_flags, rpub->sys_flags); 143 #else 144 sprintf(srv_string, "service '%s'%s%s(slot %d, ep %d, pid %d)", 145 rpub->label, srv_active_str(rp), srv_version_str(rp), 146 slot_nr, rpub->endpoint, rp->r_pid); 147 #endif 148 149 return srv_string; 150 } 151 152 /*===========================================================================* 153 * reply * 154 *===========================================================================*/ 155 void reply(who, rp, m_ptr) 156 endpoint_t who; /* replyee */ 157 struct rproc *rp; /* replyee slot (if any) */ 158 message *m_ptr; /* reply message */ 159 { 160 int r; /* send status */ 161 162 /* No need to actually reply to RS */ 163 if(who == RS_PROC_NR) { 164 return; 165 } 166 167 if(rs_verbose && rp) 168 printf("RS: %s being replied to\n", srv_to_string(rp)); 169 170 r = ipc_sendnb(who, m_ptr); /* send the message */ 171 if (r != OK) 172 printf("RS: unable to send reply to %d: %d\n", who, r); 173 } 174 175 /*===========================================================================* 176 * late_reply * 177 *===========================================================================*/ 178 void late_reply(rp, code) 179 struct rproc *rp; /* pointer to process slot */ 180 int code; /* status code */ 181 { 182 /* If a caller is waiting for a reply, unblock it. */ 183 if(rp->r_flags & RS_LATEREPLY) { 184 message m; 185 m.m_type = code; 186 if(rs_verbose) 187 printf("RS: %s late reply %d to %d for request %d\n", 188 srv_to_string(rp), code, rp->r_caller, rp->r_caller_request); 189 190 reply(rp->r_caller, NULL, &m); 191 rp->r_flags &= ~RS_LATEREPLY; 192 } 193 } 194 195 /*===========================================================================* 196 * rs_isokendpt * 197 *===========================================================================*/ 198 int rs_isokendpt(endpoint_t endpoint, int *proc) 199 { 200 *proc = _ENDPOINT_P(endpoint); 201 if(*proc < -NR_TASKS || *proc >= NR_PROCS) 202 return EINVAL; 203 204 return OK; 205 } 206 207 /*===========================================================================* 208 * sched_init_proc * 209 *===========================================================================*/ 210 int sched_init_proc(struct rproc *rp) 211 { 212 int s; 213 int is_usr_proc; 214 215 /* Make sure user processes have no scheduler. PM deals with them. */ 216 is_usr_proc = !(rp->r_priv.s_flags & SYS_PROC); 217 if(is_usr_proc) assert(rp->r_scheduler == NONE); 218 if(!is_usr_proc) assert(rp->r_scheduler != NONE); 219 220 /* Start scheduling for the given process. */ 221 if ((s = sched_start(rp->r_scheduler, rp->r_pub->endpoint, 222 RS_PROC_NR, rp->r_priority, rp->r_quantum, rp->r_cpu, 223 &rp->r_scheduler)) != OK) { 224 return s; 225 } 226 227 return s; 228 } 229 230 /*===========================================================================* 231 * update_sig_mgrs * 232 *===========================================================================*/ 233 int update_sig_mgrs(struct rproc *rp, endpoint_t sig_mgr, 234 endpoint_t bak_sig_mgr) 235 { 236 int r; 237 struct rprocpub *rpub; 238 239 rpub = rp->r_pub; 240 241 if(rs_verbose) 242 printf("RS: %s updates signal managers: %d%s / %d\n", srv_to_string(rp), 243 sig_mgr == SELF ? rpub->endpoint : sig_mgr, 244 sig_mgr == SELF ? "(SELF)" : "", 245 bak_sig_mgr == NONE ? -1 : bak_sig_mgr); 246 247 /* Synch privilege structure with the kernel. */ 248 if ((r = sys_getpriv(&rp->r_priv, rpub->endpoint)) != OK) { 249 printf("unable to synch privilege structure: %d", r); 250 return r; 251 } 252 253 /* Set signal managers. */ 254 rp->r_priv.s_sig_mgr = sig_mgr; 255 rp->r_priv.s_bak_sig_mgr = bak_sig_mgr; 256 257 /* Update privilege structure. */ 258 r = sys_privctl(rpub->endpoint, SYS_PRIV_UPDATE_SYS, &rp->r_priv); 259 if(r != OK) { 260 printf("unable to update privilege structure: %d", r); 261 return r; 262 } 263 264 return OK; 265 } 266 267