os_info.c 4.1 KB

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