1 /*
2  * SPU file system -- system call stubs
3  *
4  * (C) Copyright IBM Deutschland Entwicklung GmbH 2005
5  * (C) Copyright 2006-2007, IBM Corporation
6  *
7  * Author: Arnd Bergmann <arndb@de.ibm.com>
8  *
9  * This program is free software; you can redistribute it and/or modify
10  * it under the terms of the GNU General Public License as published by
11  * the Free Software Foundation; either version 2, or (at your option)
12  * any later version.
13  *
14  * This program is distributed in the hope that it will be useful,
15  * but WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17  * GNU General Public License for more details.
18  *
19  * You should have received a copy of the GNU General Public License
20  * along with this program; if not, write to the Free Software
21  * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
22  */
23 #include <linux/file.h>
24 #include <linux/fs.h>
25 #include <linux/module.h>
26 #include <linux/syscalls.h>
27 #include <linux/rcupdate.h>
28 #include <linux/binfmts.h>
29 #include <linux/syscalls.h>
30 
31 #include <asm/spu.h>
32 
33 /* protected by rcu */
34 static struct spufs_calls *spufs_calls;
35 
36 #ifdef CONFIG_SPU_FS_MODULE
37 
38 static inline struct spufs_calls *spufs_calls_get(void)
39 {
40 	struct spufs_calls *calls = NULL;
41 
42 	rcu_read_lock();
43 	calls = rcu_dereference(spufs_calls);
44 	if (calls && !try_module_get(calls->owner))
45 		calls = NULL;
46 	rcu_read_unlock();
47 
48 	return calls;
49 }
50 
51 static inline void spufs_calls_put(struct spufs_calls *calls)
52 {
53 	BUG_ON(calls != spufs_calls);
54 
55 	/* we don't need to rcu this, as we hold a reference to the module */
56 	module_put(spufs_calls->owner);
57 }
58 
59 #else /* !defined CONFIG_SPU_FS_MODULE */
60 
61 static inline struct spufs_calls *spufs_calls_get(void)
62 {
63 	return spufs_calls;
64 }
65 
66 static inline void spufs_calls_put(struct spufs_calls *calls) { }
67 
68 #endif /* CONFIG_SPU_FS_MODULE */
69 
70 SYSCALL_DEFINE4(spu_create, const char __user *, name, unsigned int, flags,
71 	umode_t, mode, int, neighbor_fd)
72 {
73 	long ret;
74 	struct spufs_calls *calls;
75 
76 	calls = spufs_calls_get();
77 	if (!calls)
78 		return -ENOSYS;
79 
80 	if (flags & SPU_CREATE_AFFINITY_SPU) {
81 		struct fd neighbor = fdget(neighbor_fd);
82 		ret = -EBADF;
83 		if (neighbor.file) {
84 			ret = calls->create_thread(name, flags, mode, neighbor.file);
85 			fdput(neighbor);
86 		}
87 	} else
88 		ret = calls->create_thread(name, flags, mode, NULL);
89 
90 	spufs_calls_put(calls);
91 	return ret;
92 }
93 
94 SYSCALL_DEFINE3(spu_run,int, fd, __u32 __user *, unpc, __u32 __user *, ustatus)
95 {
96 	long ret;
97 	struct fd arg;
98 	struct spufs_calls *calls;
99 
100 	calls = spufs_calls_get();
101 	if (!calls)
102 		return -ENOSYS;
103 
104 	ret = -EBADF;
105 	arg = fdget(fd);
106 	if (arg.file) {
107 		ret = calls->spu_run(arg.file, unpc, ustatus);
108 		fdput(arg);
109 	}
110 
111 	spufs_calls_put(calls);
112 	return ret;
113 }
114 
115 #ifdef CONFIG_COREDUMP
116 int elf_coredump_extra_notes_size(void)
117 {
118 	struct spufs_calls *calls;
119 	int ret;
120 
121 	calls = spufs_calls_get();
122 	if (!calls)
123 		return 0;
124 
125 	ret = calls->coredump_extra_notes_size();
126 
127 	spufs_calls_put(calls);
128 
129 	return ret;
130 }
131 
132 int elf_coredump_extra_notes_write(struct coredump_params *cprm)
133 {
134 	struct spufs_calls *calls;
135 	int ret;
136 
137 	calls = spufs_calls_get();
138 	if (!calls)
139 		return 0;
140 
141 	ret = calls->coredump_extra_notes_write(cprm);
142 
143 	spufs_calls_put(calls);
144 
145 	return ret;
146 }
147 #endif
148 
149 void notify_spus_active(void)
150 {
151 	struct spufs_calls *calls;
152 
153 	calls = spufs_calls_get();
154 	if (!calls)
155 		return;
156 
157 	calls->notify_spus_active();
158 	spufs_calls_put(calls);
159 
160 	return;
161 }
162 
163 int register_spu_syscalls(struct spufs_calls *calls)
164 {
165 	if (spufs_calls)
166 		return -EBUSY;
167 
168 	rcu_assign_pointer(spufs_calls, calls);
169 	return 0;
170 }
171 EXPORT_SYMBOL_GPL(register_spu_syscalls);
172 
173 void unregister_spu_syscalls(struct spufs_calls *calls)
174 {
175 	BUG_ON(spufs_calls->owner != calls->owner);
176 	RCU_INIT_POINTER(spufs_calls, NULL);
177 	synchronize_rcu();
178 }
179 EXPORT_SYMBOL_GPL(unregister_spu_syscalls);
180