blob: 75530d99eda6b831f363170ada2822140f38c29c [file] [log] [blame]
Arnd Bergmann67207b92005-11-15 15:53:48 -05001/*
2 * SPU file system -- system call stubs
3 *
4 * (C) Copyright IBM Deutschland Entwicklung GmbH 2005
Michael Ellerman48cad412007-09-19 14:38:12 +10005 * (C) Copyright 2006-2007, IBM Corporation
Arnd Bergmann67207b92005-11-15 15:53:48 -05006 *
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>
Michael Ellermane5501492007-09-19 14:38:12 +100024#include <linux/fs.h>
Arnd Bergmann67207b92005-11-15 15:53:48 -050025#include <linux/module.h>
26#include <linux/syscalls.h>
Jeremy Kerr98f06972007-09-19 14:38:12 +100027#include <linux/rcupdate.h>
Arnd Bergmann67207b92005-11-15 15:53:48 -050028
29#include <asm/spu.h>
30
Jeremy Kerr98f06972007-09-19 14:38:12 +100031/* protected by rcu */
32static struct spufs_calls *spufs_calls;
Arnd Bergmann67207b92005-11-15 15:53:48 -050033
Jeremy Kerr98f06972007-09-19 14:38:12 +100034#ifdef CONFIG_SPU_FS_MODULE
35
36static inline struct spufs_calls *spufs_calls_get(void)
37{
38 struct spufs_calls *calls = NULL;
39
40 rcu_read_lock();
41 calls = rcu_dereference(spufs_calls);
42 if (calls && !try_module_get(calls->owner))
43 calls = NULL;
44 rcu_read_unlock();
45
46 return calls;
47}
48
49static inline void spufs_calls_put(struct spufs_calls *calls)
50{
51 BUG_ON(calls != spufs_calls);
52
53 /* we don't need to rcu this, as we hold a reference to the module */
54 module_put(spufs_calls->owner);
55}
56
57#else /* !defined CONFIG_SPU_FS_MODULE */
58
59static inline struct spufs_calls *spufs_calls_get(void)
60{
61 return spufs_calls;
62}
63
64static inline void spufs_calls_put(struct spufs_calls *calls) { }
65
66#endif /* CONFIG_SPU_FS_MODULE */
Arnd Bergmann67207b92005-11-15 15:53:48 -050067
68asmlinkage long sys_spu_create(const char __user *name,
Arnd Bergmann8e68e2f2007-07-20 21:39:47 +020069 unsigned int flags, mode_t mode, int neighbor_fd)
Arnd Bergmann67207b92005-11-15 15:53:48 -050070{
71 long ret;
Arnd Bergmann8e68e2f2007-07-20 21:39:47 +020072 struct file *neighbor;
73 int fput_needed;
Jeremy Kerr98f06972007-09-19 14:38:12 +100074 struct spufs_calls *calls;
Arnd Bergmann67207b92005-11-15 15:53:48 -050075
Jeremy Kerr98f06972007-09-19 14:38:12 +100076 calls = spufs_calls_get();
77 if (!calls)
78 return -ENOSYS;
79
80 if (flags & SPU_CREATE_AFFINITY_SPU) {
81 ret = -EBADF;
82 neighbor = fget_light(neighbor_fd, &fput_needed);
83 if (neighbor) {
84 ret = calls->create_thread(name, flags, mode, neighbor);
85 fput_light(neighbor, fput_needed);
Arnd Bergmann8e68e2f2007-07-20 21:39:47 +020086 }
Jeremy Kerr98f06972007-09-19 14:38:12 +100087 } else
88 ret = calls->create_thread(name, flags, mode, NULL);
89
90 spufs_calls_put(calls);
Arnd Bergmann67207b92005-11-15 15:53:48 -050091 return ret;
92}
93
94asmlinkage long sys_spu_run(int fd, __u32 __user *unpc, __u32 __user *ustatus)
95{
96 long ret;
97 struct file *filp;
98 int fput_needed;
Jeremy Kerr98f06972007-09-19 14:38:12 +100099 struct spufs_calls *calls;
Arnd Bergmann67207b92005-11-15 15:53:48 -0500100
Jeremy Kerr98f06972007-09-19 14:38:12 +1000101 calls = spufs_calls_get();
102 if (!calls)
103 return -ENOSYS;
104
105 ret = -EBADF;
106 filp = fget_light(fd, &fput_needed);
107 if (filp) {
108 ret = calls->spu_run(filp, unpc, ustatus);
109 fput_light(filp, fput_needed);
Arnd Bergmann67207b92005-11-15 15:53:48 -0500110 }
Jeremy Kerr98f06972007-09-19 14:38:12 +1000111
112 spufs_calls_put(calls);
Arnd Bergmann67207b92005-11-15 15:53:48 -0500113 return ret;
114}
115
Michael Ellermane5501492007-09-19 14:38:12 +1000116int elf_coredump_extra_notes_size(void)
Michael Ellerman48cad412007-09-19 14:38:12 +1000117{
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
Michael Ellermane5501492007-09-19 14:38:12 +1000132int elf_coredump_extra_notes_write(struct file *file, loff_t *foffset)
Michael Ellerman48cad412007-09-19 14:38:12 +1000133{
134 struct spufs_calls *calls;
Michael Ellerman7af14432007-09-19 14:38:12 +1000135 int ret;
Michael Ellerman48cad412007-09-19 14:38:12 +1000136
137 calls = spufs_calls_get();
138 if (!calls)
Michael Ellermane5501492007-09-19 14:38:12 +1000139 return 0;
Michael Ellerman48cad412007-09-19 14:38:12 +1000140
Michael Ellerman7af14432007-09-19 14:38:12 +1000141 ret = calls->coredump_extra_notes_write(file, foffset);
Michael Ellerman48cad412007-09-19 14:38:12 +1000142
143 spufs_calls_put(calls);
Michael Ellermane5501492007-09-19 14:38:12 +1000144
Michael Ellerman7af14432007-09-19 14:38:12 +1000145 return ret;
Michael Ellerman48cad412007-09-19 14:38:12 +1000146}
147
Bob Nelsonaed3a8c2007-12-15 01:27:30 +1100148void notify_spus_active(void)
149{
150 struct spufs_calls *calls;
151
152 calls = spufs_calls_get();
153 if (!calls)
154 return;
155
156 calls->notify_spus_active();
157 spufs_calls_put(calls);
158
159 return;
160}
161
Arnd Bergmann67207b92005-11-15 15:53:48 -0500162int register_spu_syscalls(struct spufs_calls *calls)
163{
Jeremy Kerr98f06972007-09-19 14:38:12 +1000164 if (spufs_calls)
Arnd Bergmann67207b92005-11-15 15:53:48 -0500165 return -EBUSY;
166
Jeremy Kerr98f06972007-09-19 14:38:12 +1000167 rcu_assign_pointer(spufs_calls, calls);
Arnd Bergmann67207b92005-11-15 15:53:48 -0500168 return 0;
169}
170EXPORT_SYMBOL_GPL(register_spu_syscalls);
171
172void unregister_spu_syscalls(struct spufs_calls *calls)
173{
Jeremy Kerr98f06972007-09-19 14:38:12 +1000174 BUG_ON(spufs_calls->owner != calls->owner);
175 rcu_assign_pointer(spufs_calls, NULL);
176 synchronize_rcu();
Arnd Bergmann67207b92005-11-15 15:53:48 -0500177}
178EXPORT_SYMBOL_GPL(unregister_spu_syscalls);