1 // SPDX-License-Identifier: GPL-2.0-or-later
5 * (C) Copyright 2006 IBM Corp.
7 * Author: Dwayne Grant McConnell <decimal@us.ibm.com>
10 #include <linux/elf.h>
11 #include <linux/file.h>
12 #include <linux/fdtable.h>
14 #include <linux/gfp.h>
15 #include <linux/list.h>
16 #include <linux/syscalls.h>
17 #include <linux/coredump.h>
18 #include <linux/binfmts.h>
20 #include <linux/uaccess.h>
24 static ssize_t do_coredump_read(int num, struct spu_context *ctx, void *buffer,
25 size_t size, loff_t *off)
30 if (spufs_coredump_read[num].read)
31 return spufs_coredump_read[num].read(ctx, buffer, size, off);
33 data = spufs_coredump_read[num].get(ctx);
34 ret = snprintf(buffer, size, "0x%.16llx", data);
37 return ++ret; /* count trailing NULL */
40 static int spufs_ctx_note_size(struct spu_context *ctx, int dfd)
46 for (i = 0; spufs_coredump_read[i].name != NULL; i++) {
47 name = spufs_coredump_read[i].name;
48 sz = spufs_coredump_read[i].size;
50 sprintf(fullname, "SPU/%d/%s", dfd, name);
52 total += sizeof(struct elf_note);
53 total += roundup(strlen(fullname) + 1, 4);
54 total += roundup(sz, 4);
60 static int match_context(const void *v, struct file *file, unsigned fd)
62 struct spu_context *ctx;
63 if (file->f_op != &spufs_context_fops)
65 ctx = SPUFS_I(file_inode(file))->i_ctx;
66 if (ctx->flags & SPU_CREATE_NOSCHED)
72 * The additional architecture-specific notes for Cell are various
73 * context files in the spu context.
75 * This function iterates over all open file descriptors and sees
76 * if they are a directory in spufs. In that case we use spufs
77 * internal functionality to dump them without needing to actually
81 * descriptor table is not shared, so files can't change or go away.
83 static struct spu_context *coredump_next_context(int *fd)
86 int n = iterate_fd(current->files, *fd, match_context, NULL);
91 return SPUFS_I(file_inode(file))->i_ctx;
94 int spufs_coredump_extra_notes_size(void)
96 struct spu_context *ctx;
100 while ((ctx = coredump_next_context(&fd)) != NULL) {
101 rc = spu_acquire_saved(ctx);
104 rc = spufs_ctx_note_size(ctx, fd);
105 spu_release_saved(ctx);
111 /* start searching the next fd next time */
118 static int spufs_arch_write_note(struct spu_context *ctx, int i,
119 struct coredump_params *cprm, int dfd)
122 int sz, rc, total = 0;
123 const int bufsz = PAGE_SIZE;
125 char fullname[80], *buf;
129 buf = (void *)get_zeroed_page(GFP_KERNEL);
133 name = spufs_coredump_read[i].name;
134 sz = spufs_coredump_read[i].size;
136 sprintf(fullname, "SPU/%d/%s", dfd, name);
137 en.n_namesz = strlen(fullname) + 1;
141 if (!dump_emit(cprm, &en, sizeof(en)))
144 if (!dump_emit(cprm, fullname, en.n_namesz))
147 if (!dump_align(cprm, 4))
151 rc = do_coredump_read(i, ctx, buf, bufsz, &pos);
153 if (!dump_emit(cprm, buf, rc))
157 } while (rc == bufsz && total < sz);
162 skip = roundup(cprm->pos - total + sz, 4) - cprm->pos;
163 if (!dump_skip(cprm, skip))
168 free_page((unsigned long)buf);
171 free_page((unsigned long)buf);
175 int spufs_coredump_extra_notes_write(struct coredump_params *cprm)
177 struct spu_context *ctx;
181 while ((ctx = coredump_next_context(&fd)) != NULL) {
182 rc = spu_acquire_saved(ctx);
186 for (j = 0; spufs_coredump_read[j].name != NULL; j++) {
187 rc = spufs_arch_write_note(ctx, j, cprm, fd);
189 spu_release_saved(ctx);
194 spu_release_saved(ctx);
196 /* start searching the next fd next time */