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 int spufs_ctx_note_size(struct spu_context *ctx, int dfd)
30 for (i = 0; spufs_coredump_read[i].name != NULL; i++) {
31 name = spufs_coredump_read[i].name;
32 sz = spufs_coredump_read[i].size;
34 sprintf(fullname, "SPU/%d/%s", dfd, name);
36 total += sizeof(struct elf_note);
37 total += roundup(strlen(fullname) + 1, 4);
38 total += roundup(sz, 4);
44 static int match_context(const void *v, struct file *file, unsigned fd)
46 struct spu_context *ctx;
47 if (file->f_op != &spufs_context_fops)
49 ctx = SPUFS_I(file_inode(file))->i_ctx;
50 if (ctx->flags & SPU_CREATE_NOSCHED)
56 * The additional architecture-specific notes for Cell are various
57 * context files in the spu context.
59 * This function iterates over all open file descriptors and sees
60 * if they are a directory in spufs. In that case we use spufs
61 * internal functionality to dump them without needing to actually
65 * descriptor table is not shared, so files can't change or go away.
67 static struct spu_context *coredump_next_context(int *fd)
69 struct spu_context *ctx = NULL;
71 int n = iterate_fd(current->files, *fd, match_context, NULL);
77 file = lookup_fdget_rcu(*fd);
80 ctx = SPUFS_I(file_inode(file))->i_ctx;
88 int spufs_coredump_extra_notes_size(void)
90 struct spu_context *ctx;
94 while ((ctx = coredump_next_context(&fd)) != NULL) {
95 rc = spu_acquire_saved(ctx);
101 rc = spufs_ctx_note_size(ctx, fd);
102 spu_release_saved(ctx);
104 put_spu_context(ctx);
110 /* start searching the next fd next time */
112 put_spu_context(ctx);
118 static int spufs_arch_write_note(struct spu_context *ctx, int i,
119 struct coredump_params *cprm, int dfd)
121 size_t sz = spufs_coredump_read[i].size;
126 sprintf(fullname, "SPU/%d/%s", dfd, spufs_coredump_read[i].name);
127 en.n_namesz = strlen(fullname) + 1;
131 if (!dump_emit(cprm, &en, sizeof(en)))
133 if (!dump_emit(cprm, fullname, en.n_namesz))
135 if (!dump_align(cprm, 4))
138 if (spufs_coredump_read[i].dump) {
139 ret = spufs_coredump_read[i].dump(ctx, cprm);
145 ret = snprintf(buf, sizeof(buf), "0x%.16llx",
146 spufs_coredump_read[i].get(ctx));
147 if (ret >= sizeof(buf))
150 /* count trailing the NULL: */
151 if (!dump_emit(cprm, buf, ret + 1))
155 dump_skip_to(cprm, roundup(cprm->pos - ret + sz, 4));
159 int spufs_coredump_extra_notes_write(struct coredump_params *cprm)
161 struct spu_context *ctx;
165 while ((ctx = coredump_next_context(&fd)) != NULL) {
166 rc = spu_acquire_saved(ctx);
170 for (j = 0; spufs_coredump_read[j].name != NULL; j++) {
171 rc = spufs_arch_write_note(ctx, j, cprm, fd);
173 spu_release_saved(ctx);
178 spu_release_saved(ctx);
180 /* start searching the next fd next time */