2 * Copyright (c) 2017 Hans Petter Selasky
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
8 * 1. Redistributions of source code must retain the above copyright
9 * notice unmodified, this list of conditions, and the following
11 * 2. Redistributions in binary form must reproduce the above copyright
12 * notice, this list of conditions and the following disclaimer in the
13 * documentation and/or other materials provided with the distribution.
15 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
16 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
17 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
18 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
19 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
20 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
21 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
22 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
23 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
24 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27 #include <sys/cdefs.h>
28 __FBSDID("$FreeBSD$");
30 #include <linux/compat.h>
31 #include <linux/completion.h>
33 #include <linux/kthread.h>
35 #include <sys/kernel.h>
36 #include <sys/eventhandler.h>
37 #include <sys/malloc.h>
39 static eventhandler_tag linuxkpi_thread_dtor_tag;
41 static MALLOC_DEFINE(M_LINUX_CURRENT, "linuxcurrent", "LinuxKPI task structure");
44 linux_alloc_current(struct thread *td, int flags)
47 struct thread *td_other;
48 struct task_struct *ts;
49 struct task_struct *ts_other;
51 struct mm_struct *mm_other;
53 MPASS(td->td_lkpi_task == NULL);
55 ts = malloc(sizeof(*ts), M_LINUX_CURRENT, flags | M_ZERO);
59 mm = malloc(sizeof(*mm), M_LINUX_CURRENT, flags | M_ZERO);
61 free(ts, M_LINUX_CURRENT);
65 /* setup new task structure */
66 atomic_set(&ts->kthread_flags, 0);
68 ts->comm = td->td_name;
70 atomic_set(&ts->usage, 1);
71 ts->state = TASK_RUNNING;
72 init_completion(&ts->parked);
73 init_completion(&ts->exited);
77 /* check if another thread already has a mm_struct */
79 FOREACH_THREAD_IN_PROC(proc, td_other) {
80 ts_other = td_other->td_lkpi_task;
84 mm_other = ts_other->mm;
88 /* try to share other mm_struct */
89 if (atomic_inc_not_zero(&mm_other->mm_users)) {
90 /* set mm_struct pointer */
96 /* use allocated mm_struct as a fallback */
98 /* setup new mm_struct */
99 init_rwsem(&mm->mmap_sem);
100 atomic_set(&mm->mm_count, 1);
101 atomic_set(&mm->mm_users, 1);
102 /* set mm_struct pointer */
104 /* clear pointer to not free memory */
108 /* store pointer to task struct */
109 td->td_lkpi_task = ts;
112 /* free mm_struct pointer, if any */
113 free(mm, M_LINUX_CURRENT);
119 linux_get_task_mm(struct task_struct *task)
121 struct mm_struct *mm;
125 atomic_inc(&mm->mm_users);
132 linux_mm_dtor(struct mm_struct *mm)
134 free(mm, M_LINUX_CURRENT);
138 linux_free_current(struct task_struct *ts)
141 free(ts, M_LINUX_CURRENT);
145 linuxkpi_thread_dtor(void *arg __unused, struct thread *td)
147 struct task_struct *ts;
149 ts = td->td_lkpi_task;
153 td->td_lkpi_task = NULL;
158 linux_pid_task(pid_t pid)
163 /* try to find corresponding thread */
164 td = tdfind(pid, -1);
166 struct task_struct *ts = td->td_lkpi_task;
167 PROC_UNLOCK(td->td_proc);
171 /* try to find corresponding procedure */
174 FOREACH_THREAD_IN_PROC(p, td) {
175 struct task_struct *ts = td->td_lkpi_task;
187 linux_get_pid_task(pid_t pid)
192 /* try to find corresponding thread */
193 td = tdfind(pid, -1);
195 struct task_struct *ts = td->td_lkpi_task;
198 PROC_UNLOCK(td->td_proc);
202 /* try to find corresponding procedure */
205 FOREACH_THREAD_IN_PROC(p, td) {
206 struct task_struct *ts = td->td_lkpi_task;
219 linux_current_init(void *arg __unused)
221 linuxkpi_thread_dtor_tag = EVENTHANDLER_REGISTER(thread_dtor,
222 linuxkpi_thread_dtor, NULL, EVENTHANDLER_PRI_ANY);
224 SYSINIT(linux_current, SI_SUB_EVENTHANDLER, SI_ORDER_SECOND, linux_current_init, NULL);
227 linux_current_uninit(void *arg __unused)
230 struct task_struct *ts;
233 sx_slock(&allproc_lock);
234 FOREACH_PROC_IN_SYSTEM(p) {
236 FOREACH_THREAD_IN_PROC(p, td) {
237 if ((ts = td->td_lkpi_task) != NULL) {
238 td->td_lkpi_task = NULL;
244 sx_sunlock(&allproc_lock);
246 EVENTHANDLER_DEREGISTER(thread_dtor, linuxkpi_thread_dtor_tag);
248 SYSUNINIT(linux_current, SI_SUB_EVENTHANDLER, SI_ORDER_SECOND, linux_current_uninit, NULL);