]>
Commit | Line | Data |
---|---|---|
bf1ab978 DGM |
1 | /* |
2 | * SPU core dump code | |
3 | * | |
4 | * (C) Copyright 2006 IBM Corp. | |
5 | * | |
6 | * Author: Dwayne Grant McConnell <decimal@us.ibm.com> | |
7 | * | |
8 | * This program is free software; you can redistribute it and/or modify | |
9 | * it under the terms of the GNU General Public License as published by | |
10 | * the Free Software Foundation; either version 2, or (at your option) | |
11 | * any later version. | |
12 | * | |
13 | * This program is distributed in the hope that it will be useful, | |
14 | * but WITHOUT ANY WARRANTY; without even the implied warranty of | |
15 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
16 | * GNU General Public License for more details. | |
17 | * | |
18 | * You should have received a copy of the GNU General Public License | |
19 | * along with this program; if not, write to the Free Software | |
20 | * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. | |
21 | */ | |
22 | ||
23 | #include <linux/elf.h> | |
24 | #include <linux/file.h> | |
25 | #include <linux/fs.h> | |
26 | #include <linux/list.h> | |
27 | #include <linux/module.h> | |
28 | #include <linux/syscalls.h> | |
29 | ||
30 | #include <asm/uaccess.h> | |
31 | ||
32 | #include "spufs.h" | |
33 | ||
d464fb44 | 34 | static ssize_t do_coredump_read(int num, struct spu_context *ctx, void *buffer, |
bf1ab978 DGM |
35 | size_t size, loff_t *off) |
36 | { | |
37 | u64 data; | |
38 | int ret; | |
39 | ||
40 | if (spufs_coredump_read[num].read) | |
41 | return spufs_coredump_read[num].read(ctx, buffer, size, off); | |
42 | ||
43 | data = spufs_coredump_read[num].get(ctx); | |
d464fb44 ME |
44 | ret = snprintf(buffer, size, "0x%.16lx", data); |
45 | if (ret >= size) | |
46 | return size; | |
47 | return ++ret; /* count trailing NULL */ | |
bf1ab978 DGM |
48 | } |
49 | ||
50 | /* | |
51 | * These are the only things you should do on a core-file: use only these | |
52 | * functions to write out all the necessary info. | |
53 | */ | |
7af1443a | 54 | static int spufs_dump_write(struct file *file, const void *addr, int nr, loff_t *foffset) |
bf1ab978 | 55 | { |
9e25ae6d | 56 | unsigned long limit = current->signal->rlim[RLIMIT_CORE].rlim_cur; |
7af1443a ME |
57 | ssize_t written; |
58 | ||
9e25ae6d ME |
59 | if (*foffset + nr > limit) |
60 | return -EIO; | |
61 | ||
7af1443a ME |
62 | written = file->f_op->write(file, addr, nr, &file->f_pos); |
63 | *foffset += written; | |
64 | ||
65 | if (written != nr) | |
66 | return -EIO; | |
67 | ||
68 | return 0; | |
bf1ab978 DGM |
69 | } |
70 | ||
7af1443a ME |
71 | static int spufs_dump_align(struct file *file, char *buf, loff_t new_off, |
72 | loff_t *foffset) | |
bf1ab978 | 73 | { |
7af1443a ME |
74 | int rc, size; |
75 | ||
76 | size = min((loff_t)PAGE_SIZE, new_off - *foffset); | |
77 | memset(buf, 0, size); | |
78 | ||
79 | rc = 0; | |
80 | while (rc == 0 && new_off > *foffset) { | |
81 | size = min((loff_t)PAGE_SIZE, new_off - *foffset); | |
82 | rc = spufs_dump_write(file, buf, size, foffset); | |
83 | } | |
84 | ||
85 | return rc; | |
bf1ab978 DGM |
86 | } |
87 | ||
f9b7bbe7 | 88 | static int spufs_ctx_note_size(struct spu_context *ctx, int dfd) |
bf1ab978 | 89 | { |
f9b7bbe7 | 90 | int i, sz, total = 0; |
bf1ab978 DGM |
91 | char *name; |
92 | char fullname[80]; | |
93 | ||
936d5bf1 | 94 | for (i = 0; spufs_coredump_read[i].name != NULL; i++) { |
bf1ab978 DGM |
95 | name = spufs_coredump_read[i].name; |
96 | sz = spufs_coredump_read[i].size; | |
97 | ||
98 | sprintf(fullname, "SPU/%d/%s", dfd, name); | |
99 | ||
100 | total += sizeof(struct elf_note); | |
101 | total += roundup(strlen(fullname) + 1, 4); | |
59000b53 | 102 | total += roundup(sz, 4); |
bf1ab978 DGM |
103 | } |
104 | ||
105 | return total; | |
106 | } | |
107 | ||
bf1ab978 DGM |
108 | /* |
109 | * The additional architecture-specific notes for Cell are various | |
110 | * context files in the spu context. | |
111 | * | |
112 | * This function iterates over all open file descriptors and sees | |
113 | * if they are a directory in spufs. In that case we use spufs | |
114 | * internal functionality to dump them without needing to actually | |
115 | * open the files. | |
116 | */ | |
a595ed66 | 117 | static struct spu_context *coredump_next_context(int *fd) |
bf1ab978 DGM |
118 | { |
119 | struct fdtable *fdt = files_fdtable(current->files); | |
a595ed66 ME |
120 | struct file *file; |
121 | struct spu_context *ctx = NULL; | |
122 | ||
123 | for (; *fd < fdt->max_fds; (*fd)++) { | |
124 | if (!FD_ISSET(*fd, fdt->open_fds)) | |
125 | continue; | |
126 | ||
127 | file = fcheck(*fd); | |
128 | ||
129 | if (!file || file->f_op != &spufs_context_fops) | |
130 | continue; | |
131 | ||
132 | ctx = SPUFS_I(file->f_dentry->d_inode)->i_ctx; | |
133 | if (ctx->flags & SPU_CREATE_NOSCHED) | |
134 | continue; | |
135 | ||
136 | /* start searching the next fd next time we're called */ | |
137 | (*fd)++; | |
138 | break; | |
139 | } | |
140 | ||
141 | return ctx; | |
142 | } | |
143 | ||
48cad41f | 144 | int spufs_coredump_extra_notes_size(void) |
a595ed66 ME |
145 | { |
146 | struct spu_context *ctx; | |
147 | int size = 0, rc, fd; | |
148 | ||
149 | fd = 0; | |
150 | while ((ctx = coredump_next_context(&fd)) != NULL) { | |
c9101bdb CH |
151 | rc = spu_acquire_saved(ctx); |
152 | if (rc) | |
153 | break; | |
f9b7bbe7 | 154 | rc = spufs_ctx_note_size(ctx, fd); |
9a5080f1 | 155 | spu_release_saved(ctx); |
a595ed66 ME |
156 | if (rc < 0) |
157 | break; | |
158 | ||
159 | size += rc; | |
bf1ab978 DGM |
160 | } |
161 | ||
162 | return size; | |
163 | } | |
164 | ||
7af1443a ME |
165 | static int spufs_arch_write_note(struct spu_context *ctx, int i, |
166 | struct file *file, int dfd, loff_t *foffset) | |
bf1ab978 | 167 | { |
bf1ab978 | 168 | loff_t pos = 0; |
7af1443a | 169 | int sz, rc, nread, total = 0; |
6cf21792 | 170 | const int bufsz = PAGE_SIZE; |
bf1ab978 DGM |
171 | char *name; |
172 | char fullname[80], *buf; | |
173 | struct elf_note en; | |
174 | ||
6cf21792 | 175 | buf = (void *)get_zeroed_page(GFP_KERNEL); |
bf1ab978 | 176 | if (!buf) |
7af1443a | 177 | return -ENOMEM; |
bf1ab978 | 178 | |
bf1ab978 | 179 | name = spufs_coredump_read[i].name; |
59000b53 | 180 | sz = spufs_coredump_read[i].size; |
bf1ab978 | 181 | |
bf1ab978 DGM |
182 | sprintf(fullname, "SPU/%d/%s", dfd, name); |
183 | en.n_namesz = strlen(fullname) + 1; | |
184 | en.n_descsz = sz; | |
185 | en.n_type = NT_SPU; | |
186 | ||
7af1443a ME |
187 | rc = spufs_dump_write(file, &en, sizeof(en), foffset); |
188 | if (rc) | |
6cf21792 | 189 | goto out; |
7af1443a ME |
190 | |
191 | rc = spufs_dump_write(file, fullname, en.n_namesz, foffset); | |
192 | if (rc) | |
6cf21792 | 193 | goto out; |
7af1443a ME |
194 | |
195 | rc = spufs_dump_align(file, buf, roundup(*foffset, 4), foffset); | |
196 | if (rc) | |
6cf21792 | 197 | goto out; |
bf1ab978 DGM |
198 | |
199 | do { | |
7af1443a ME |
200 | nread = do_coredump_read(i, ctx, buf, bufsz, &pos); |
201 | if (nread > 0) { | |
202 | rc = spufs_dump_write(file, buf, nread, foffset); | |
203 | if (rc) | |
6cf21792 | 204 | goto out; |
7af1443a | 205 | total += nread; |
bf1ab978 | 206 | } |
7af1443a ME |
207 | } while (nread == bufsz && total < sz); |
208 | ||
209 | if (nread < 0) { | |
210 | rc = nread; | |
211 | goto out; | |
212 | } | |
213 | ||
214 | rc = spufs_dump_align(file, buf, roundup(*foffset - total + sz, 4), | |
215 | foffset); | |
bf1ab978 | 216 | |
6cf21792 AB |
217 | out: |
218 | free_page((unsigned long)buf); | |
7af1443a | 219 | return rc; |
bf1ab978 DGM |
220 | } |
221 | ||
7af1443a | 222 | int spufs_coredump_extra_notes_write(struct file *file, loff_t *foffset) |
bf1ab978 | 223 | { |
f9b7bbe7 | 224 | struct spu_context *ctx; |
7af1443a | 225 | int fd, j, rc; |
f9b7bbe7 ME |
226 | |
227 | fd = 0; | |
228 | while ((ctx = coredump_next_context(&fd)) != NULL) { | |
c9101bdb CH |
229 | rc = spu_acquire_saved(ctx); |
230 | if (rc) | |
231 | return rc; | |
bf1ab978 | 232 | |
7af1443a ME |
233 | for (j = 0; spufs_coredump_read[j].name != NULL; j++) { |
234 | rc = spufs_arch_write_note(ctx, j, file, fd, foffset); | |
235 | if (rc) { | |
236 | spu_release_saved(ctx); | |
237 | return rc; | |
238 | } | |
239 | } | |
f9b7bbe7 ME |
240 | |
241 | spu_release_saved(ctx); | |
bf1ab978 | 242 | } |
7af1443a ME |
243 | |
244 | return 0; | |
bf1ab978 | 245 | } |