os_info.c 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * OS info memory interface
  4. *
  5. * Copyright IBM Corp. 2012
  6. * Author(s): Michael Holzheu <holzheu@linux.vnet.ibm.com>
  7. */
  8. #define KMSG_COMPONENT "os_info"
  9. #define pr_fmt(fmt) KMSG_COMPONENT ": " fmt
  10. #include <linux/crash_dump.h>
  11. #include <linux/kernel.h>
  12. #include <linux/slab.h>
  13. #include <asm/checksum.h>
  14. #include <asm/abs_lowcore.h>
  15. #include <asm/os_info.h>
  16. #include <asm/physmem_info.h>
  17. #include <asm/maccess.h>
  18. #include <asm/asm-offsets.h>
  19. #include <asm/ipl.h>
  20. /*
  21. * OS info structure has to be page aligned
  22. */
  23. static struct os_info os_info __page_aligned_data;
  24. /*
  25. * Compute checksum over OS info structure
  26. */
  27. u32 os_info_csum(struct os_info *os_info)
  28. {
  29. int size = sizeof(*os_info) - offsetof(struct os_info, version_major);
  30. return (__force u32)cksm(&os_info->version_major, size, 0);
  31. }
  32. /*
  33. * Add crashkernel info to OS info and update checksum
  34. */
  35. void os_info_crashkernel_add(unsigned long base, unsigned long size)
  36. {
  37. os_info.crashkernel_addr = (u64)(unsigned long)base;
  38. os_info.crashkernel_size = (u64)(unsigned long)size;
  39. os_info.csum = os_info_csum(&os_info);
  40. }
  41. /*
  42. * Add OS info data entry and update checksum
  43. */
  44. void os_info_entry_add_data(int nr, void *ptr, u64 size)
  45. {
  46. os_info.entry[nr].addr = __pa(ptr);
  47. os_info.entry[nr].size = size;
  48. os_info.entry[nr].csum = (__force u32)cksm(ptr, size, 0);
  49. os_info.csum = os_info_csum(&os_info);
  50. }
  51. /*
  52. * Add OS info value entry and update checksum
  53. */
  54. void os_info_entry_add_val(int nr, u64 value)
  55. {
  56. os_info.entry[nr].val = value;
  57. os_info.entry[nr].size = 0;
  58. os_info.entry[nr].csum = 0;
  59. os_info.csum = os_info_csum(&os_info);
  60. }
  61. /*
  62. * Initialize OS info structure and set lowcore pointer
  63. */
  64. void __init os_info_init(void)
  65. {
  66. struct lowcore *abs_lc;
  67. BUILD_BUG_ON(sizeof(struct os_info) != PAGE_SIZE);
  68. os_info.version_major = OS_INFO_VERSION_MAJOR;
  69. os_info.version_minor = OS_INFO_VERSION_MINOR;
  70. os_info.magic = OS_INFO_MAGIC;
  71. os_info_entry_add_val(OS_INFO_IDENTITY_BASE, __identity_base);
  72. os_info_entry_add_val(OS_INFO_KASLR_OFFSET, kaslr_offset());
  73. os_info_entry_add_val(OS_INFO_KASLR_OFF_PHYS, __kaslr_offset_phys);
  74. os_info_entry_add_val(OS_INFO_VMEMMAP, (unsigned long)vmemmap);
  75. os_info_entry_add_val(OS_INFO_AMODE31_START, AMODE31_START);
  76. os_info_entry_add_val(OS_INFO_AMODE31_END, AMODE31_END);
  77. os_info_entry_add_val(OS_INFO_IMAGE_START, (unsigned long)_stext);
  78. os_info_entry_add_val(OS_INFO_IMAGE_END, (unsigned long)_end);
  79. os_info_entry_add_val(OS_INFO_IMAGE_PHYS, __pa_symbol(_stext));
  80. os_info.csum = os_info_csum(&os_info);
  81. abs_lc = get_abs_lowcore();
  82. abs_lc->os_info = __pa(&os_info);
  83. put_abs_lowcore(abs_lc);
  84. }
  85. #ifdef CONFIG_CRASH_DUMP
  86. static struct os_info *os_info_old;
  87. /*
  88. * Allocate and copy OS info entry from oldmem
  89. */
  90. static void os_info_old_alloc(int nr, int align)
  91. {
  92. unsigned long addr, size = 0;
  93. char *buf, *buf_align, *msg;
  94. u32 csum;
  95. addr = os_info_old->entry[nr].addr;
  96. if (!addr) {
  97. msg = "not available";
  98. goto fail;
  99. }
  100. size = os_info_old->entry[nr].size;
  101. buf = kmalloc(size + align - 1, GFP_KERNEL);
  102. if (!buf) {
  103. msg = "alloc failed";
  104. goto fail;
  105. }
  106. buf_align = PTR_ALIGN(buf, align);
  107. if (copy_oldmem_kernel(buf_align, addr, size)) {
  108. msg = "copy failed";
  109. goto fail_free;
  110. }
  111. csum = (__force u32)cksm(buf_align, size, 0);
  112. if (csum != os_info_old->entry[nr].csum) {
  113. msg = "checksum failed";
  114. goto fail_free;
  115. }
  116. os_info_old->entry[nr].addr = (u64)(unsigned long)buf_align;
  117. msg = "copied";
  118. goto out;
  119. fail_free:
  120. kfree(buf);
  121. fail:
  122. os_info_old->entry[nr].addr = 0;
  123. out:
  124. pr_info("entry %i: %s (addr=0x%lx size=%lu)\n",
  125. nr, msg, addr, size);
  126. }
  127. /*
  128. * Initialize os info and os info entries from oldmem
  129. */
  130. static void os_info_old_init(void)
  131. {
  132. static int os_info_init;
  133. unsigned long addr;
  134. if (os_info_init)
  135. return;
  136. if (!oldmem_data.start && !is_ipl_type_dump())
  137. goto fail;
  138. if (copy_oldmem_kernel(&addr, __LC_OS_INFO, sizeof(addr)))
  139. goto fail;
  140. if (addr == 0 || addr % PAGE_SIZE)
  141. goto fail;
  142. os_info_old = kzalloc(sizeof(*os_info_old), GFP_KERNEL);
  143. if (!os_info_old)
  144. goto fail;
  145. if (copy_oldmem_kernel(os_info_old, addr, sizeof(*os_info_old)))
  146. goto fail_free;
  147. if (os_info_old->magic != OS_INFO_MAGIC)
  148. goto fail_free;
  149. if (os_info_old->csum != os_info_csum(os_info_old))
  150. goto fail_free;
  151. if (os_info_old->version_major > OS_INFO_VERSION_MAJOR)
  152. goto fail_free;
  153. os_info_old_alloc(OS_INFO_VMCOREINFO, 1);
  154. os_info_old_alloc(OS_INFO_REIPL_BLOCK, 1);
  155. pr_info("crashkernel: addr=0x%lx size=%lu\n",
  156. (unsigned long) os_info_old->crashkernel_addr,
  157. (unsigned long) os_info_old->crashkernel_size);
  158. os_info_init = 1;
  159. return;
  160. fail_free:
  161. kfree(os_info_old);
  162. fail:
  163. os_info_init = 1;
  164. os_info_old = NULL;
  165. }
  166. /*
  167. * Return pointer to os infor entry and its size
  168. */
  169. void *os_info_old_entry(int nr, unsigned long *size)
  170. {
  171. os_info_old_init();
  172. if (!os_info_old)
  173. return NULL;
  174. if (!os_info_old->entry[nr].addr)
  175. return NULL;
  176. *size = (unsigned long) os_info_old->entry[nr].size;
  177. return (void *)(unsigned long)os_info_old->entry[nr].addr;
  178. }
  179. #endif