]>
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> | |
9f3acc31 | 25 | #include <linux/fdtable.h> |
bf1ab978 | 26 | #include <linux/fs.h> |
5a0e3ad6 | 27 | #include <linux/gfp.h> |
bf1ab978 | 28 | #include <linux/list.h> |
bf1ab978 | 29 | #include <linux/syscalls.h> |
cdc3d562 AV |
30 | #include <linux/coredump.h> |
31 | #include <linux/binfmts.h> | |
bf1ab978 | 32 | |
7c0f6ba6 | 33 | #include <linux/uaccess.h> |
bf1ab978 DGM |
34 | |
35 | #include "spufs.h" | |
36 | ||
d464fb44 | 37 | static ssize_t do_coredump_read(int num, struct spu_context *ctx, void *buffer, |
bf1ab978 DGM |
38 | size_t size, loff_t *off) |
39 | { | |
40 | u64 data; | |
41 | int ret; | |
42 | ||
43 | if (spufs_coredump_read[num].read) | |
44 | return spufs_coredump_read[num].read(ctx, buffer, size, off); | |
45 | ||
46 | data = spufs_coredump_read[num].get(ctx); | |
9477e455 | 47 | ret = snprintf(buffer, size, "0x%.16llx", data); |
d464fb44 ME |
48 | if (ret >= size) |
49 | return size; | |
50 | return ++ret; /* count trailing NULL */ | |
bf1ab978 DGM |
51 | } |
52 | ||
f9b7bbe7 | 53 | static int spufs_ctx_note_size(struct spu_context *ctx, int dfd) |
bf1ab978 | 54 | { |
f9b7bbe7 | 55 | int i, sz, total = 0; |
bf1ab978 DGM |
56 | char *name; |
57 | char fullname[80]; | |
58 | ||
936d5bf1 | 59 | for (i = 0; spufs_coredump_read[i].name != NULL; i++) { |
bf1ab978 DGM |
60 | name = spufs_coredump_read[i].name; |
61 | sz = spufs_coredump_read[i].size; | |
62 | ||
63 | sprintf(fullname, "SPU/%d/%s", dfd, name); | |
64 | ||
65 | total += sizeof(struct elf_note); | |
66 | total += roundup(strlen(fullname) + 1, 4); | |
59000b53 | 67 | total += roundup(sz, 4); |
bf1ab978 DGM |
68 | } |
69 | ||
70 | return total; | |
71 | } | |
72 | ||
2be7fd55 AV |
73 | static int match_context(const void *v, struct file *file, unsigned fd) |
74 | { | |
75 | struct spu_context *ctx; | |
76 | if (file->f_op != &spufs_context_fops) | |
77 | return 0; | |
496ad9aa | 78 | ctx = SPUFS_I(file_inode(file))->i_ctx; |
2be7fd55 AV |
79 | if (ctx->flags & SPU_CREATE_NOSCHED) |
80 | return 0; | |
81 | return fd + 1; | |
82 | } | |
83 | ||
bf1ab978 DGM |
84 | /* |
85 | * The additional architecture-specific notes for Cell are various | |
86 | * context files in the spu context. | |
87 | * | |
88 | * This function iterates over all open file descriptors and sees | |
89 | * if they are a directory in spufs. In that case we use spufs | |
90 | * internal functionality to dump them without needing to actually | |
91 | * open the files. | |
92 | */ | |
2be7fd55 AV |
93 | /* |
94 | * descriptor table is not shared, so files can't change or go away. | |
95 | */ | |
a595ed66 | 96 | static struct spu_context *coredump_next_context(int *fd) |
bf1ab978 | 97 | { |
a595ed66 | 98 | struct file *file; |
2be7fd55 AV |
99 | int n = iterate_fd(current->files, *fd, match_context, NULL); |
100 | if (!n) | |
101 | return NULL; | |
102 | *fd = n - 1; | |
103 | file = fcheck(*fd); | |
496ad9aa | 104 | return SPUFS_I(file_inode(file))->i_ctx; |
a595ed66 ME |
105 | } |
106 | ||
48cad41f | 107 | int spufs_coredump_extra_notes_size(void) |
a595ed66 ME |
108 | { |
109 | struct spu_context *ctx; | |
110 | int size = 0, rc, fd; | |
111 | ||
112 | fd = 0; | |
113 | while ((ctx = coredump_next_context(&fd)) != NULL) { | |
c9101bdb CH |
114 | rc = spu_acquire_saved(ctx); |
115 | if (rc) | |
116 | break; | |
f9b7bbe7 | 117 | rc = spufs_ctx_note_size(ctx, fd); |
9a5080f1 | 118 | spu_release_saved(ctx); |
a595ed66 ME |
119 | if (rc < 0) |
120 | break; | |
121 | ||
122 | size += rc; | |
ada397e9 GS |
123 | |
124 | /* start searching the next fd next time */ | |
125 | fd++; | |
bf1ab978 DGM |
126 | } |
127 | ||
128 | return size; | |
129 | } | |
130 | ||
7af1443a | 131 | static int spufs_arch_write_note(struct spu_context *ctx, int i, |
cdc3d562 | 132 | struct coredump_params *cprm, int dfd) |
bf1ab978 | 133 | { |
bf1ab978 | 134 | loff_t pos = 0; |
7b1f4020 | 135 | int sz, rc, total = 0; |
6cf21792 | 136 | const int bufsz = PAGE_SIZE; |
bf1ab978 DGM |
137 | char *name; |
138 | char fullname[80], *buf; | |
139 | struct elf_note en; | |
a0083939 | 140 | size_t skip; |
bf1ab978 | 141 | |
6cf21792 | 142 | buf = (void *)get_zeroed_page(GFP_KERNEL); |
bf1ab978 | 143 | if (!buf) |
7af1443a | 144 | return -ENOMEM; |
bf1ab978 | 145 | |
bf1ab978 | 146 | name = spufs_coredump_read[i].name; |
59000b53 | 147 | sz = spufs_coredump_read[i].size; |
bf1ab978 | 148 | |
bf1ab978 DGM |
149 | sprintf(fullname, "SPU/%d/%s", dfd, name); |
150 | en.n_namesz = strlen(fullname) + 1; | |
151 | en.n_descsz = sz; | |
152 | en.n_type = NT_SPU; | |
153 | ||
7b1f4020 AV |
154 | if (!dump_emit(cprm, &en, sizeof(en))) |
155 | goto Eio; | |
7af1443a | 156 | |
7b1f4020 AV |
157 | if (!dump_emit(cprm, fullname, en.n_namesz)) |
158 | goto Eio; | |
7af1443a | 159 | |
22a8cb82 | 160 | if (!dump_align(cprm, 4)) |
7b1f4020 | 161 | goto Eio; |
bf1ab978 DGM |
162 | |
163 | do { | |
7b1f4020 AV |
164 | rc = do_coredump_read(i, ctx, buf, bufsz, &pos); |
165 | if (rc > 0) { | |
166 | if (!dump_emit(cprm, buf, rc)) | |
167 | goto Eio; | |
168 | total += rc; | |
bf1ab978 | 169 | } |
7b1f4020 | 170 | } while (rc == bufsz && total < sz); |
7af1443a | 171 | |
7b1f4020 | 172 | if (rc < 0) |
7af1443a | 173 | goto out; |
bf1ab978 | 174 | |
1607f09c | 175 | skip = roundup(cprm->pos - total + sz, 4) - cprm->pos; |
a0083939 | 176 | if (!dump_skip(cprm, skip)) |
7b1f4020 | 177 | goto Eio; |
99acc9be ME |
178 | |
179 | rc = 0; | |
6cf21792 AB |
180 | out: |
181 | free_page((unsigned long)buf); | |
7af1443a | 182 | return rc; |
7b1f4020 AV |
183 | Eio: |
184 | free_page((unsigned long)buf); | |
185 | return -EIO; | |
bf1ab978 DGM |
186 | } |
187 | ||
cdc3d562 | 188 | int spufs_coredump_extra_notes_write(struct coredump_params *cprm) |
bf1ab978 | 189 | { |
f9b7bbe7 | 190 | struct spu_context *ctx; |
7af1443a | 191 | int fd, j, rc; |
f9b7bbe7 ME |
192 | |
193 | fd = 0; | |
194 | while ((ctx = coredump_next_context(&fd)) != NULL) { | |
c9101bdb CH |
195 | rc = spu_acquire_saved(ctx); |
196 | if (rc) | |
197 | return rc; | |
bf1ab978 | 198 | |
7af1443a | 199 | for (j = 0; spufs_coredump_read[j].name != NULL; j++) { |
cdc3d562 | 200 | rc = spufs_arch_write_note(ctx, j, cprm, fd); |
7af1443a ME |
201 | if (rc) { |
202 | spu_release_saved(ctx); | |
203 | return rc; | |
204 | } | |
205 | } | |
f9b7bbe7 ME |
206 | |
207 | spu_release_saved(ctx); | |
ada397e9 GS |
208 | |
209 | /* start searching the next fd next time */ | |
210 | fd++; | |
bf1ab978 | 211 | } |
7af1443a ME |
212 | |
213 | return 0; | |
bf1ab978 | 214 | } |