coredump.c 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215
  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. #include <linux/elf.h>
  23. #include <linux/file.h>
  24. #include <linux/fdtable.h>
  25. #include <linux/fs.h>
  26. #include <linux/gfp.h>
  27. #include <linux/list.h>
  28. #include <linux/syscalls.h>
  29. #include <linux/coredump.h>
  30. #include <linux/binfmts.h>
  31. #include <asm/uaccess.h>
  32. #include "spufs.h"
  33. static ssize_t do_coredump_read(int num, struct spu_context *ctx, void *buffer,
  34. size_t size, loff_t *off)
  35. {
  36. u64 data;
  37. int ret;
  38. if (spufs_coredump_read[num].read)
  39. return spufs_coredump_read[num].read(ctx, buffer, size, off);
  40. data = spufs_coredump_read[num].get(ctx);
  41. ret = snprintf(buffer, size, "0x%.16llx", data);
  42. if (ret >= size)
  43. return size;
  44. return ++ret; /* count trailing NULL */
  45. }
  46. static int spufs_ctx_note_size(struct spu_context *ctx, int dfd)
  47. {
  48. int i, sz, total = 0;
  49. char *name;
  50. char fullname[80];
  51. for (i = 0; spufs_coredump_read[i].name != NULL; i++) {
  52. name = spufs_coredump_read[i].name;
  53. sz = spufs_coredump_read[i].size;
  54. sprintf(fullname, "SPU/%d/%s", dfd, name);
  55. total += sizeof(struct elf_note);
  56. total += roundup(strlen(fullname) + 1, 4);
  57. total += roundup(sz, 4);
  58. }
  59. return total;
  60. }
  61. static int match_context(const void *v, struct file *file, unsigned fd)
  62. {
  63. struct spu_context *ctx;
  64. if (file->f_op != &spufs_context_fops)
  65. return 0;
  66. ctx = SPUFS_I(file_inode(file))->i_ctx;
  67. if (ctx->flags & SPU_CREATE_NOSCHED)
  68. return 0;
  69. return fd + 1;
  70. }
  71. /*
  72. * The additional architecture-specific notes for Cell are various
  73. * context files in the spu context.
  74. *
  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
  78. * open the files.
  79. */
  80. /*
  81. * descriptor table is not shared, so files can't change or go away.
  82. */
  83. static struct spu_context *coredump_next_context(int *fd)
  84. {
  85. struct file *file;
  86. int n = iterate_fd(current->files, *fd, match_context, NULL);
  87. if (!n)
  88. return NULL;
  89. *fd = n - 1;
  90. file = fcheck(*fd);
  91. return SPUFS_I(file_inode(file))->i_ctx;
  92. }
  93. int spufs_coredump_extra_notes_size(void)
  94. {
  95. struct spu_context *ctx;
  96. int size = 0, rc, fd;
  97. fd = 0;
  98. while ((ctx = coredump_next_context(&fd)) != NULL) {
  99. rc = spu_acquire_saved(ctx);
  100. if (rc)
  101. break;
  102. rc = spufs_ctx_note_size(ctx, fd);
  103. spu_release_saved(ctx);
  104. if (rc < 0)
  105. break;
  106. size += rc;
  107. /* start searching the next fd next time */
  108. fd++;
  109. }
  110. return size;
  111. }
  112. static int spufs_arch_write_note(struct spu_context *ctx, int i,
  113. struct coredump_params *cprm, int dfd)
  114. {
  115. loff_t pos = 0;
  116. int sz, rc, total = 0;
  117. const int bufsz = PAGE_SIZE;
  118. char *name;
  119. char fullname[80], *buf;
  120. struct elf_note en;
  121. size_t skip;
  122. buf = (void *)get_zeroed_page(GFP_KERNEL);
  123. if (!buf)
  124. return -ENOMEM;
  125. name = spufs_coredump_read[i].name;
  126. sz = spufs_coredump_read[i].size;
  127. sprintf(fullname, "SPU/%d/%s", dfd, name);
  128. en.n_namesz = strlen(fullname) + 1;
  129. en.n_descsz = sz;
  130. en.n_type = NT_SPU;
  131. if (!dump_emit(cprm, &en, sizeof(en)))
  132. goto Eio;
  133. if (!dump_emit(cprm, fullname, en.n_namesz))
  134. goto Eio;
  135. if (!dump_align(cprm, 4))
  136. goto Eio;
  137. do {
  138. rc = do_coredump_read(i, ctx, buf, bufsz, &pos);
  139. if (rc > 0) {
  140. if (!dump_emit(cprm, buf, rc))
  141. goto Eio;
  142. total += rc;
  143. }
  144. } while (rc == bufsz && total < sz);
  145. if (rc < 0)
  146. goto out;
  147. skip = roundup(cprm->pos - total + sz, 4) - cprm->pos;
  148. if (!dump_skip(cprm, skip))
  149. goto Eio;
  150. rc = 0;
  151. out:
  152. free_page((unsigned long)buf);
  153. return rc;
  154. Eio:
  155. free_page((unsigned long)buf);
  156. return -EIO;
  157. }
  158. int spufs_coredump_extra_notes_write(struct coredump_params *cprm)
  159. {
  160. struct spu_context *ctx;
  161. int fd, j, rc;
  162. fd = 0;
  163. while ((ctx = coredump_next_context(&fd)) != NULL) {
  164. rc = spu_acquire_saved(ctx);
  165. if (rc)
  166. return rc;
  167. for (j = 0; spufs_coredump_read[j].name != NULL; j++) {
  168. rc = spufs_arch_write_note(ctx, j, cprm, fd);
  169. if (rc) {
  170. spu_release_saved(ctx);
  171. return rc;
  172. }
  173. }
  174. spu_release_saved(ctx);
  175. /* start searching the next fd next time */
  176. fd++;
  177. }
  178. return 0;
  179. }