1 /*
2  * CDDL HEADER START
3  *
4  * The contents of this file are subject to the terms of the
5  * Common Development and Distribution License (the "License").
6  * You may not use this file except in compliance with the License.
7  *
8  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9  * or http://www.opensolaris.org/os/licensing.
10  * See the License for the specific language governing permissions
11  * and limitations under the License.
12  *
13  * When distributing Covered Code, include this CDDL HEADER in each
14  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15  * If applicable, add the following below this CDDL HEADER, with the
16  * fields enclosed by brackets "[]" replaced with your own identifying
17  * information: Portions Copyright [yyyy] [name of copyright owner]
18  *
19  * CDDL HEADER END
20  */
21 
22 /*
23  * Copyright 2006 Sun Microsystems, Inc.  All rights reserved.
24  * Use is subject to license terms.
25  */
26 /*
27  * Copyright 2015, Joyent, Inc.
28  */
29 
30 #include <sys/cpuvar.h>
31 #include <sys/stack.h>
32 #include <vm/seg_kp.h>
33 #include <sys/machparam.h>
34 #include <sys/proc.h>
35 #include <sys/pset.h>
36 #include <sys/sysmacros.h>
37 
38 /*
39  * Create and initialize an interrupt thread.
40  */
41 static void
thread_create_intr(cpu_t * cp)42 thread_create_intr(cpu_t *cp)
43 {
44 	kthread_t *tp;
45 
46 	tp = thread_create(NULL, LL_INTR_STKSZ,
47 	    (void (*)())thread_create_intr, NULL, 0, &p0, TS_ONPROC, 0);
48 
49 	/*
50 	 * Set the thread in the TS_FREE state.  The state will change
51 	 * to TS_ONPROC only while the interrupt is active.  Think of these
52 	 * as being on a private free list for the CPU.  Being TS_FREE keeps
53 	 * inactive interrupt threads out of debugger thread lists.
54 	 *
55 	 * We cannot call thread_create with TS_FREE because of the current
56 	 * checks there for ONPROC.  Fix this when thread_create takes flags.
57 	 */
58 	THREAD_FREEINTR(tp, cp);
59 
60 	/*
61 	 * Nobody should ever reference the credentials of an interrupt
62 	 * thread so make it NULL to catch any such references.
63 	 */
64 	tp->t_cred = NULL;
65 	tp->t_flag |= T_INTR_THREAD;
66 	tp->t_cpu = cp;
67 	tp->t_bound_cpu = cp;
68 	tp->t_disp_queue = cp->cpu_disp;
69 	tp->t_affinitycnt = 1;
70 	tp->t_preempt = 1;
71 
72 	/*
73 	 * Don't make a user-requested binding on this thread so that
74 	 * the processor can be offlined.
75 	 */
76 	tp->t_bind_cpu = PBIND_NONE;	/* no USER-requested binding */
77 	tp->t_bind_pset = PS_NONE;
78 
79 #if defined(__x86)
80 	tp->t_stk -= STACK_ALIGN;
81 	*(tp->t_stk) = 0;		/* terminate intr thread stack */
82 #endif
83 
84 	/*
85 	 * Link onto CPU's interrupt pool.
86 	 */
87 	tp->t_link = cp->cpu_intr_thread;
88 	cp->cpu_intr_thread = tp;
89 }
90 
91 /*
92  * Allocate a given number of interrupt threads for a given CPU.  These threads
93  * will get freed by cpu_destroy_bound_threads() when the CPU gets unconfigured.
94  *
95  * Note, high level interrupts are always serviced using cpu_intr_stack and are
96  * not allowed to block. Low level interrupts or soft-interrupts use the
97  * kthread_t's that we create through the calls to thread_create_intr().
98  */
99 void
cpu_intr_alloc(cpu_t * cp,int n)100 cpu_intr_alloc(cpu_t *cp, int n)
101 {
102 	int i;
103 
104 	for (i = 0; i < n; i++)
105 		thread_create_intr(cp);
106 
107 	cp->cpu_intr_stack = (caddr_t)segkp_get(segkp, INTR_STACK_SIZE,
108 	    KPD_HASREDZONE | KPD_NO_ANON | KPD_LOCKED) +
109 	    INTR_STACK_SIZE - SA(MINFRAME);
110 }
111