os_info.c 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168
  1. /*
  2. * OS info memory interface
  3. *
  4. * Copyright IBM Corp. 2012
  5. * Author(s): Michael Holzheu <holzheu@linux.vnet.ibm.com>
  6. */
  7. #define KMSG_COMPONENT "os_info"
  8. #define pr_fmt(fmt) KMSG_COMPONENT ": " fmt
  9. #include <linux/crash_dump.h>
  10. #include <linux/kernel.h>
  11. #include <linux/slab.h>
  12. #include <asm/checksum.h>
  13. #include <asm/lowcore.h>
  14. #include <asm/os_info.h>
  15. /*
  16. * OS info structure has to be page aligned
  17. */
  18. static struct os_info os_info __page_aligned_data;
  19. /*
  20. * Compute checksum over OS info structure
  21. */
  22. u32 os_info_csum(struct os_info *os_info)
  23. {
  24. int size = sizeof(*os_info) - offsetof(struct os_info, version_major);
  25. return csum_partial(&os_info->version_major, size, 0);
  26. }
  27. /*
  28. * Add crashkernel info to OS info and update checksum
  29. */
  30. void os_info_crashkernel_add(unsigned long base, unsigned long size)
  31. {
  32. os_info.crashkernel_addr = (u64)(unsigned long)base;
  33. os_info.crashkernel_size = (u64)(unsigned long)size;
  34. os_info.csum = os_info_csum(&os_info);
  35. }
  36. /*
  37. * Add OS info entry and update checksum
  38. */
  39. void os_info_entry_add(int nr, void *ptr, u64 size)
  40. {
  41. os_info.entry[nr].addr = (u64)(unsigned long)ptr;
  42. os_info.entry[nr].size = size;
  43. os_info.entry[nr].csum = csum_partial(ptr, size, 0);
  44. os_info.csum = os_info_csum(&os_info);
  45. }
  46. /*
  47. * Initialize OS info struture and set lowcore pointer
  48. */
  49. void __init os_info_init(void)
  50. {
  51. void *ptr = &os_info;
  52. os_info.version_major = OS_INFO_VERSION_MAJOR;
  53. os_info.version_minor = OS_INFO_VERSION_MINOR;
  54. os_info.magic = OS_INFO_MAGIC;
  55. os_info.csum = os_info_csum(&os_info);
  56. mem_assign_absolute(S390_lowcore.os_info, (unsigned long) ptr);
  57. }
  58. #ifdef CONFIG_CRASH_DUMP
  59. static struct os_info *os_info_old;
  60. /*
  61. * Allocate and copy OS info entry from oldmem
  62. */
  63. static void os_info_old_alloc(int nr, int align)
  64. {
  65. unsigned long addr, size = 0;
  66. char *buf, *buf_align, *msg;
  67. u32 csum;
  68. addr = os_info_old->entry[nr].addr;
  69. if (!addr) {
  70. msg = "not available";
  71. goto fail;
  72. }
  73. size = os_info_old->entry[nr].size;
  74. buf = kmalloc(size + align - 1, GFP_KERNEL);
  75. if (!buf) {
  76. msg = "alloc failed";
  77. goto fail;
  78. }
  79. buf_align = PTR_ALIGN(buf, align);
  80. if (copy_from_oldmem(buf_align, (void *) addr, size)) {
  81. msg = "copy failed";
  82. goto fail_free;
  83. }
  84. csum = csum_partial(buf_align, size, 0);
  85. if (csum != os_info_old->entry[nr].csum) {
  86. msg = "checksum failed";
  87. goto fail_free;
  88. }
  89. os_info_old->entry[nr].addr = (u64)(unsigned long)buf_align;
  90. msg = "copied";
  91. goto out;
  92. fail_free:
  93. kfree(buf);
  94. fail:
  95. os_info_old->entry[nr].addr = 0;
  96. out:
  97. pr_info("entry %i: %s (addr=0x%lx size=%lu)\n",
  98. nr, msg, addr, size);
  99. }
  100. /*
  101. * Initialize os info and os info entries from oldmem
  102. */
  103. static void os_info_old_init(void)
  104. {
  105. static int os_info_init;
  106. unsigned long addr;
  107. if (os_info_init)
  108. return;
  109. if (!OLDMEM_BASE)
  110. goto fail;
  111. if (copy_from_oldmem(&addr, &S390_lowcore.os_info, sizeof(addr)))
  112. goto fail;
  113. if (addr == 0 || addr % PAGE_SIZE)
  114. goto fail;
  115. os_info_old = kzalloc(sizeof(*os_info_old), GFP_KERNEL);
  116. if (!os_info_old)
  117. goto fail;
  118. if (copy_from_oldmem(os_info_old, (void *) addr, sizeof(*os_info_old)))
  119. goto fail_free;
  120. if (os_info_old->magic != OS_INFO_MAGIC)
  121. goto fail_free;
  122. if (os_info_old->csum != os_info_csum(os_info_old))
  123. goto fail_free;
  124. if (os_info_old->version_major > OS_INFO_VERSION_MAJOR)
  125. goto fail_free;
  126. os_info_old_alloc(OS_INFO_VMCOREINFO, 1);
  127. os_info_old_alloc(OS_INFO_REIPL_BLOCK, 1);
  128. pr_info("crashkernel: addr=0x%lx size=%lu\n",
  129. (unsigned long) os_info_old->crashkernel_addr,
  130. (unsigned long) os_info_old->crashkernel_size);
  131. os_info_init = 1;
  132. return;
  133. fail_free:
  134. kfree(os_info_old);
  135. fail:
  136. os_info_init = 1;
  137. os_info_old = NULL;
  138. }
  139. /*
  140. * Return pointer to os infor entry and its size
  141. */
  142. void *os_info_old_entry(int nr, unsigned long *size)
  143. {
  144. os_info_old_init();
  145. if (!os_info_old)
  146. return NULL;
  147. if (!os_info_old->entry[nr].addr)
  148. return NULL;
  149. *size = (unsigned long) os_info_old->entry[nr].size;
  150. return (void *)(unsigned long)os_info_old->entry[nr].addr;
  151. }
  152. #endif