minidump_log.c 40 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * Copyright (c) 2017-2021, The Linux Foundation. All rights reserved.
  4. * Copyright (c) 2022-2023 Qualcomm Innovation Center, Inc. All rights reserved.
  5. */
  6. #include <linux/cache.h>
  7. #include <linux/freezer.h>
  8. #include <linux/bitops.h>
  9. #include <linux/module.h>
  10. #include <linux/kernel.h>
  11. #include <linux/init.h>
  12. #include <linux/kallsyms.h>
  13. #include <linux/rbtree.h>
  14. #include <linux/sched.h>
  15. #include <linux/slab.h>
  16. #include <linux/thread_info.h>
  17. #include <soc/qcom/minidump.h>
  18. #include <asm/page.h>
  19. #include <asm/memory.h>
  20. #include <asm/sections.h>
  21. #include <asm/stacktrace.h>
  22. #include <linux/mm.h>
  23. #include <linux/ratelimit.h>
  24. #include <linux/notifier.h>
  25. #include <linux/sizes.h>
  26. #include <linux/sched/task.h>
  27. #include <linux/suspend.h>
  28. #include <linux/vmalloc.h>
  29. #include <linux/panic_notifier.h>
  30. #include "debug_symbol.h"
  31. #include <linux/samsung/debug/sec_debug.h>
  32. #ifdef CONFIG_QCOM_MINIDUMP_PSTORE
  33. #include <linux/math64.h>
  34. #include <linux/of.h>
  35. #include <linux/of_address.h>
  36. #include <linux/of_reserved_mem.h>
  37. #endif
  38. #ifdef CONFIG_QCOM_MINIDUMP_PANIC_DUMP
  39. #include <linux/bits.h>
  40. #include <linux/sched/prio.h>
  41. #include <linux/seq_buf.h>
  42. #include <linux/debugfs.h>
  43. #include <asm/memory.h>
  44. #include <linux/sched/cputime.h>
  45. #include "../../../kernel/sched/sched.h"
  46. #include <linux/sched/walt.h>
  47. #include <linux/kdebug.h>
  48. #include <linux/thread_info.h>
  49. #include <asm/ptrace.h>
  50. #include <linux/uaccess.h>
  51. #include <linux/percpu.h>
  52. #include <linux/module.h>
  53. #include <linux/cma.h>
  54. #include <linux/dma-map-ops.h>
  55. #include <linux/sched/clock.h>
  56. #include <trace/hooks/cpufreq.h>
  57. #ifdef CONFIG_QCOM_MINIDUMP_PANIC_CPU_CONTEXT
  58. #include <trace/hooks/debug.h>
  59. #endif
  60. #include "minidump_memory.h"
  61. #endif
  62. #include "../../../kernel/time/tick-internal.h"
  63. #ifdef CONFIG_QCOM_DYN_MINIDUMP_STACK
  64. #include <trace/events/sched.h>
  65. #ifdef CONFIG_VMAP_STACK
  66. #define STACK_NUM_PAGES (THREAD_SIZE / PAGE_SIZE)
  67. #else
  68. #define STACK_NUM_PAGES 1
  69. #endif /* !CONFIG_VMAP_STACK */
  70. struct md_stack_cpu_data {
  71. int stack_mdidx[STACK_NUM_PAGES];
  72. struct md_region stack_mdr[STACK_NUM_PAGES];
  73. } ____cacheline_aligned_in_smp;
  74. static int md_current_stack_init __read_mostly;
  75. static DEFINE_PER_CPU_SHARED_ALIGNED(struct md_stack_cpu_data, md_stack_data);
  76. struct md_suspend_context_data {
  77. int task_mdno;
  78. int stack_mdidx[STACK_NUM_PAGES];
  79. struct md_region stack_mdr[STACK_NUM_PAGES];
  80. struct md_region task_mdr;
  81. bool init;
  82. };
  83. static struct md_suspend_context_data md_suspend_context;
  84. #endif
  85. static bool is_vmap_stack __read_mostly;
  86. #ifdef CONFIG_QCOM_MINIDUMP_FTRACE
  87. #include <trace/hooks/ftrace_dump.h>
  88. #include <linux/ring_buffer.h>
  89. #include <linux/trace_seq.h>
  90. #define MD_FTRACE_BUF_SIZE SZ_2M
  91. static char *md_ftrace_buf_addr;
  92. static size_t md_ftrace_buf_current;
  93. static bool minidump_ftrace_in_oops;
  94. static bool minidump_ftrace_dump = true;
  95. #endif
  96. #ifdef CONFIG_QCOM_MINIDUMP_PANIC_DUMP
  97. /* Rnqueue information */
  98. #ifndef CONFIG_MINIDUMP_ALL_TASK_INFO
  99. #define MD_RUNQUEUE_PAGES 8
  100. #else
  101. #define MD_RUNQUEUE_PAGES 150
  102. #endif
  103. static bool md_in_oops_handler;
  104. static atomic_t md_handle_done;
  105. static struct seq_buf *md_runq_seq_buf;
  106. static int md_align_offset;
  107. /* CPU context information */
  108. #ifdef CONFIG_QCOM_MINIDUMP_PANIC_CPU_CONTEXT
  109. #define MD_CPU_CNTXT_PAGES 32
  110. static int die_cpu = -1;
  111. static struct seq_buf *md_cntxt_seq_buf;
  112. static DEFINE_PER_CPU(struct pt_regs, regs_before_stop);
  113. #endif
  114. /* Meminfo */
  115. #ifdef CONFIG_QCOM_MINIDUMP_PANIC_MEMORY_INFO
  116. static struct seq_buf *md_meminfo_seq_buf;
  117. /* Slabinfo */
  118. #ifdef CONFIG_SLUB_DEBUG
  119. static struct seq_buf *md_slabinfo_seq_buf;
  120. #endif
  121. #ifdef CONFIG_PAGE_OWNER
  122. size_t md_pageowner_dump_size = SZ_2M;
  123. char *md_pageowner_dump_addr;
  124. #endif
  125. #ifdef CONFIG_SLUB_DEBUG
  126. size_t md_slabowner_dump_size = SZ_2M;
  127. char *md_slabowner_dump_addr;
  128. #endif
  129. size_t md_dma_buf_info_size = SZ_256K;
  130. char *md_dma_buf_info_addr;
  131. size_t md_dma_buf_procs_size = SZ_256K;
  132. char *md_dma_buf_procs_addr;
  133. #endif
  134. /* Modules information */
  135. #ifdef CONFIG_MODULES
  136. #define MD_MODULE_PAGES 8
  137. static struct seq_buf *md_mod_info_seq_buf;
  138. static DEFINE_SPINLOCK(md_modules_lock);
  139. static int n_modump;
  140. static char *key_modules[10];
  141. module_param_array(key_modules, charp, &n_modump, 0644);
  142. #endif /* CONFIG_MODULES */
  143. #endif
  144. #define FREQ_LOG_MAX 10
  145. static int register_stack_entry(struct md_region *ksp_entry, u64 sp, u64 size)
  146. {
  147. struct page *sp_page;
  148. int entry;
  149. ksp_entry->virt_addr = sp;
  150. ksp_entry->size = size;
  151. if (is_vmap_stack) {
  152. sp_page = vmalloc_to_page((const void *) sp);
  153. ksp_entry->phys_addr = page_to_phys(sp_page);
  154. } else {
  155. ksp_entry->phys_addr = virt_to_phys((uintptr_t *)sp);
  156. }
  157. entry = msm_minidump_add_region(ksp_entry);
  158. if (entry < 0)
  159. printk_deferred("Failed to add stack of entry %s in Minidump\n",
  160. ksp_entry->name);
  161. return entry;
  162. }
  163. static void register_kernel_sections(void)
  164. {
  165. struct md_region ksec_entry;
  166. char *data_name = "KDATABSS";
  167. char *rodata_name = "KROAIDATA";
  168. size_t static_size;
  169. void __percpu *base;
  170. unsigned int cpu;
  171. void *_sdata, *__bss_stop;
  172. void *start_ro, *end_ro;
  173. _sdata = DEBUG_SYMBOL_LOOKUP(_sdata);
  174. __bss_stop = DEBUG_SYMBOL_LOOKUP(__bss_stop);
  175. base = DEBUG_SYMBOL_LOOKUP(__per_cpu_start);
  176. static_size = (size_t)(DEBUG_SYMBOL_LOOKUP(__per_cpu_end) - base);
  177. strscpy(ksec_entry.name, data_name, sizeof(ksec_entry.name));
  178. ksec_entry.virt_addr = (u64)_sdata;
  179. ksec_entry.phys_addr = virt_to_phys(_sdata);
  180. ksec_entry.size = roundup((__bss_stop - _sdata), 4);
  181. if (msm_minidump_add_region(&ksec_entry) < 0)
  182. pr_err("Failed to add data section in Minidump\n");
  183. start_ro = DEBUG_SYMBOL_LOOKUP(__start_ro_after_init);
  184. end_ro = DEBUG_SYMBOL_LOOKUP(__end_ro_after_init);
  185. strscpy(ksec_entry.name, rodata_name, sizeof(ksec_entry.name));
  186. ksec_entry.virt_addr = (uintptr_t)start_ro;
  187. ksec_entry.phys_addr = virt_to_phys(start_ro);
  188. ksec_entry.size = roundup((end_ro - start_ro), 4);
  189. if (msm_minidump_add_region(&ksec_entry) < 0)
  190. pr_err("Failed to add rodata section in Minidump\n");
  191. /* Add percpu static sections */
  192. for_each_possible_cpu(cpu) {
  193. void *start = per_cpu_ptr(base, cpu);
  194. memset(&ksec_entry, 0, sizeof(ksec_entry));
  195. scnprintf(ksec_entry.name, sizeof(ksec_entry.name),
  196. "KSPERCPU%d", cpu);
  197. ksec_entry.virt_addr = (uintptr_t)start;
  198. ksec_entry.phys_addr = per_cpu_ptr_to_phys(start);
  199. ksec_entry.size = static_size;
  200. if (msm_minidump_add_region(&ksec_entry) < 0)
  201. pr_err("Failed to add percpu sections in Minidump\n");
  202. }
  203. }
  204. static inline bool in_stack_range(
  205. u64 sp, u64 base_addr, unsigned int stack_size)
  206. {
  207. u64 min_addr = base_addr;
  208. u64 max_addr = base_addr + stack_size;
  209. return (min_addr <= sp && sp < max_addr);
  210. }
  211. static unsigned int calculate_copy_pages(u64 sp, struct vm_struct *stack_area)
  212. {
  213. u64 tsk_stack_base = (u64) stack_area->addr;
  214. u64 offset;
  215. unsigned int stack_pages, copy_pages;
  216. if (in_stack_range(sp, tsk_stack_base, get_vm_area_size(stack_area))) {
  217. offset = sp - tsk_stack_base;
  218. stack_pages = get_vm_area_size(stack_area) / PAGE_SIZE;
  219. copy_pages = stack_pages - (offset / PAGE_SIZE);
  220. } else {
  221. copy_pages = 0;
  222. }
  223. return copy_pages;
  224. }
  225. void dump_stack_minidump(u64 sp)
  226. {
  227. struct md_region ksp_entry, ktsk_entry;
  228. u32 cpu = smp_processor_id();
  229. struct vm_struct *stack_vm_area;
  230. unsigned int i, copy_pages;
  231. if (IS_ENABLED(CONFIG_QCOM_DYN_MINIDUMP_STACK))
  232. return;
  233. if (is_idle_task(current))
  234. return;
  235. is_vmap_stack = IS_ENABLED(CONFIG_VMAP_STACK);
  236. if (sp < KIMAGE_VADDR || sp > -256UL)
  237. sp = current_stack_pointer;
  238. /*
  239. * Since stacks are now allocated with vmalloc, the translation to
  240. * physical address is not a simple linear transformation like it is
  241. * for kernel logical addresses, since vmalloc creates a virtual
  242. * mapping. Thus, virt_to_phys() should not be used in this context;
  243. * instead the page table must be walked to acquire the physical
  244. * address of one page of the stack.
  245. */
  246. stack_vm_area = task_stack_vm_area(current);
  247. if (is_vmap_stack) {
  248. sp &= ~(PAGE_SIZE - 1);
  249. copy_pages = calculate_copy_pages(sp, stack_vm_area);
  250. for (i = 0; i < copy_pages; i++) {
  251. scnprintf(ksp_entry.name, sizeof(ksp_entry.name),
  252. "KSTACK%d_%d", cpu, i);
  253. (void)register_stack_entry(&ksp_entry, sp, PAGE_SIZE);
  254. sp += PAGE_SIZE;
  255. }
  256. } else {
  257. sp &= ~(THREAD_SIZE - 1);
  258. scnprintf(ksp_entry.name, sizeof(ksp_entry.name), "KSTACK%d",
  259. cpu);
  260. (void)register_stack_entry(&ksp_entry, sp, THREAD_SIZE);
  261. }
  262. scnprintf(ktsk_entry.name, sizeof(ktsk_entry.name), "KTASK%d", cpu);
  263. ktsk_entry.virt_addr = (u64)current;
  264. ktsk_entry.phys_addr = virt_to_phys((uintptr_t *)current);
  265. ktsk_entry.size = sizeof(struct task_struct);
  266. if (msm_minidump_add_region(&ktsk_entry) < 0)
  267. pr_err("Failed to add current task %d in Minidump\n", cpu);
  268. }
  269. #ifdef CONFIG_QCOM_DYN_MINIDUMP_STACK
  270. static void update_stack_entry(struct md_region *ksp_entry, u64 sp,
  271. int mdno)
  272. {
  273. struct page *sp_page;
  274. ksp_entry->virt_addr = sp;
  275. if (likely(is_vmap_stack)) {
  276. sp_page = vmalloc_to_page((const void *) sp);
  277. ksp_entry->phys_addr = page_to_phys(sp_page);
  278. } else {
  279. ksp_entry->phys_addr = virt_to_phys((uintptr_t *)sp);
  280. }
  281. if (msm_minidump_update_region(mdno, ksp_entry) < 0) {
  282. printk_deferred("Failed to update stack entry %s in minidump\n",
  283. ksp_entry->name);
  284. }
  285. }
  286. static void register_vmapped_stack(struct md_region *mdr, int *mdno,
  287. u64 sp, char *name_str, bool update)
  288. {
  289. int i;
  290. sp &= ~(PAGE_SIZE - 1);
  291. for (i = 0; i < STACK_NUM_PAGES; i++) {
  292. if (unlikely(!update)) {
  293. scnprintf(mdr->name, sizeof(mdr->name), "%s_%d",
  294. name_str, i);
  295. *mdno = register_stack_entry(mdr, sp, PAGE_SIZE);
  296. } else {
  297. update_stack_entry(mdr, sp, *mdno);
  298. }
  299. sp += PAGE_SIZE;
  300. mdr++;
  301. mdno++;
  302. }
  303. }
  304. static void register_normal_stack(struct md_region *mdr, int *mdno,
  305. u64 sp, char *name_str, bool update)
  306. {
  307. sp &= ~(THREAD_SIZE - 1);
  308. if (unlikely(!update)) {
  309. scnprintf(mdr->name, sizeof(mdr->name), name_str);
  310. *mdno = register_stack_entry(mdr, sp, THREAD_SIZE);
  311. } else {
  312. update_stack_entry(mdr, sp, *mdno);
  313. }
  314. }
  315. static void update_md_stack(struct md_region *stack_mdr,
  316. int *stack_mdno, u64 sp)
  317. {
  318. unsigned int i;
  319. int *mdno;
  320. if (likely(is_vmap_stack)) {
  321. for (i = 0; i < STACK_NUM_PAGES; i++) {
  322. mdno = stack_mdno + i;
  323. if (unlikely(*mdno < 0))
  324. return;
  325. }
  326. register_vmapped_stack(stack_mdr, stack_mdno, sp, NULL, true);
  327. } else {
  328. if (unlikely(*stack_mdno < 0))
  329. return;
  330. register_normal_stack(stack_mdr, stack_mdno, sp, NULL, true);
  331. }
  332. }
  333. static void update_md_cpu_stack(struct task_struct *tsk, u32 cpu, u64 sp)
  334. {
  335. struct md_stack_cpu_data *md_stack_cpu_d = &per_cpu(md_stack_data, cpu);
  336. if (is_idle_task(tsk) || !md_current_stack_init)
  337. return;
  338. update_md_stack(md_stack_cpu_d->stack_mdr,
  339. md_stack_cpu_d->stack_mdidx, sp);
  340. }
  341. void md_current_stack_notifer(void *ignore, bool preempt,
  342. struct task_struct *prev, struct task_struct *next,
  343. unsigned int prev_state)
  344. {
  345. u32 cpu = task_cpu(next);
  346. u64 sp = (u64)next->stack;
  347. update_md_cpu_stack(next, cpu, sp);
  348. }
  349. void md_current_stack_ipi_handler(void *data)
  350. {
  351. u32 cpu = smp_processor_id();
  352. struct vm_struct *stack_vm_area;
  353. u64 sp = current_stack_pointer;
  354. if (is_idle_task(current))
  355. return;
  356. if (likely(is_vmap_stack)) {
  357. stack_vm_area = task_stack_vm_area(current);
  358. sp = (u64)stack_vm_area->addr;
  359. }
  360. update_md_cpu_stack(current, cpu, sp);
  361. }
  362. static void update_md_current_task(struct md_region *mdr, int mdno)
  363. {
  364. mdr->virt_addr = (u64)current;
  365. mdr->phys_addr = virt_to_phys((uintptr_t *)current);
  366. if (msm_minidump_update_region(mdno, mdr) < 0)
  367. pr_err("Failed to update %s current task in minidump\n",
  368. mdr->name);
  369. }
  370. static void update_md_suspend_current_stack(void)
  371. {
  372. u64 sp = current_stack_pointer;
  373. struct vm_struct *stack_vm_area;
  374. if (likely(is_vmap_stack)) {
  375. stack_vm_area = task_stack_vm_area(current);
  376. sp = (u64)stack_vm_area->addr;
  377. }
  378. update_md_stack(md_suspend_context.stack_mdr,
  379. md_suspend_context.stack_mdidx, sp);
  380. }
  381. static void update_md_suspend_current_task(void)
  382. {
  383. if (unlikely(md_suspend_context.task_mdno < 0))
  384. return;
  385. update_md_current_task(&md_suspend_context.task_mdr,
  386. md_suspend_context.task_mdno);
  387. }
  388. static void update_md_suspend_currents(void)
  389. {
  390. if (!md_suspend_context.init)
  391. return;
  392. update_md_suspend_current_stack();
  393. update_md_suspend_current_task();
  394. }
  395. static void register_current_stack(void)
  396. {
  397. int cpu;
  398. u64 sp = current_stack_pointer;
  399. struct md_stack_cpu_data *md_stack_cpu_d;
  400. struct vm_struct *stack_vm_area;
  401. char name_str[MAX_NAME_LENGTH];
  402. /*
  403. * Since stacks are now allocated with vmalloc, the translation to
  404. * physical address is not a simple linear transformation like it is
  405. * for kernel logical addresses, since vmalloc creates a virtual
  406. * mapping. Thus, virt_to_phys() should not be used in this context;
  407. * instead the page table must be walked to acquire the physical
  408. * address of all pages of the stack.
  409. */
  410. if (likely(is_vmap_stack)) {
  411. stack_vm_area = task_stack_vm_area(current);
  412. sp = (u64)stack_vm_area->addr;
  413. }
  414. for_each_possible_cpu(cpu) {
  415. /*
  416. * Let's register dummies for now,
  417. * once system up and running, let the cpu update its currents.
  418. */
  419. md_stack_cpu_d = &per_cpu(md_stack_data, cpu);
  420. scnprintf(name_str, sizeof(name_str), "KSTACK%d", cpu);
  421. if (is_vmap_stack)
  422. register_vmapped_stack(md_stack_cpu_d->stack_mdr,
  423. md_stack_cpu_d->stack_mdidx, sp,
  424. name_str, false);
  425. else
  426. register_normal_stack(md_stack_cpu_d->stack_mdr,
  427. md_stack_cpu_d->stack_mdidx, sp,
  428. name_str, false);
  429. }
  430. register_trace_sched_switch(md_current_stack_notifer, NULL);
  431. md_current_stack_init = 1;
  432. smp_call_function(md_current_stack_ipi_handler, NULL, 1);
  433. }
  434. static void register_suspend_stack(void)
  435. {
  436. char name_str[MAX_NAME_LENGTH];
  437. u64 sp = current_stack_pointer;
  438. struct vm_struct *stack_vm_area = task_stack_vm_area(current);
  439. scnprintf(name_str, sizeof(name_str), "KSUSPSTK");
  440. if (is_vmap_stack) {
  441. sp = (u64)stack_vm_area->addr;
  442. register_vmapped_stack(md_suspend_context.stack_mdr,
  443. md_suspend_context.stack_mdidx,
  444. sp, name_str, false);
  445. } else {
  446. register_normal_stack(md_suspend_context.stack_mdr,
  447. md_suspend_context.stack_mdidx,
  448. sp, name_str, false);
  449. }
  450. }
  451. static void register_current_task(struct md_region *mdr, int *mdno,
  452. char *name_str)
  453. {
  454. scnprintf(mdr->name, sizeof(mdr->name), name_str);
  455. mdr->virt_addr = (u64)current;
  456. mdr->phys_addr = virt_to_phys((uintptr_t *)current);
  457. mdr->size = sizeof(struct task_struct);
  458. *mdno = msm_minidump_add_region(mdr);
  459. if (*mdno < 0)
  460. pr_err("Failed to add current task %s in Minidump\n",
  461. mdr->name);
  462. }
  463. static void register_suspend_current_task(void)
  464. {
  465. char name_str[MAX_NAME_LENGTH];
  466. scnprintf(name_str, sizeof(name_str), "KSUSPTASK");
  467. register_current_task(&md_suspend_context.task_mdr,
  468. &md_suspend_context.task_mdno, name_str);
  469. }
  470. static int minidump_pm_notifier(struct notifier_block *nb,
  471. unsigned long event, void *unused)
  472. {
  473. switch (event) {
  474. case PM_SUSPEND_PREPARE:
  475. update_md_suspend_currents();
  476. break;
  477. }
  478. return NOTIFY_DONE;
  479. }
  480. static struct notifier_block minidump_pm_nb = {
  481. .notifier_call = minidump_pm_notifier,
  482. };
  483. static void register_suspend_context(void)
  484. {
  485. register_suspend_stack();
  486. register_suspend_current_task();
  487. register_pm_notifier(&minidump_pm_nb);
  488. md_suspend_context.init = true;
  489. }
  490. #endif
  491. #ifdef CONFIG_ARM64
  492. static void register_irq_stack(void)
  493. {
  494. int cpu;
  495. unsigned int i;
  496. int irq_stack_pages_count;
  497. u64 irq_stack_base;
  498. struct md_region irq_sp_entry;
  499. u64 sp;
  500. u64 *irq_stack_ptr = DEBUG_SYMBOL_LOOKUP(irq_stack_ptr);
  501. for_each_possible_cpu(cpu) {
  502. irq_stack_base = *(u64 *)(per_cpu_ptr((void *)irq_stack_ptr, cpu));
  503. if (is_vmap_stack) {
  504. irq_stack_pages_count = IRQ_STACK_SIZE / PAGE_SIZE;
  505. sp = irq_stack_base & ~(PAGE_SIZE - 1);
  506. for (i = 0; i < irq_stack_pages_count; i++) {
  507. scnprintf(irq_sp_entry.name,
  508. sizeof(irq_sp_entry.name),
  509. "KISTK%d_%d", cpu, i);
  510. register_stack_entry(&irq_sp_entry, sp,
  511. PAGE_SIZE);
  512. sp += PAGE_SIZE;
  513. }
  514. } else {
  515. sp = irq_stack_base;
  516. scnprintf(irq_sp_entry.name, sizeof(irq_sp_entry.name),
  517. "KISTK%d", cpu);
  518. register_stack_entry(&irq_sp_entry, sp, IRQ_STACK_SIZE);
  519. }
  520. }
  521. }
  522. #else
  523. static inline void register_irq_stack(void) {}
  524. #endif
  525. #ifdef CONFIG_QCOM_MINIDUMP_FTRACE
  526. static void minidump_add_trace_event(char *buf, size_t size)
  527. {
  528. char *addr;
  529. if (!READ_ONCE(md_ftrace_buf_addr) ||
  530. (size > (size_t)MD_FTRACE_BUF_SIZE))
  531. return;
  532. if ((md_ftrace_buf_current + size) > (size_t)MD_FTRACE_BUF_SIZE)
  533. md_ftrace_buf_current = 0;
  534. addr = md_ftrace_buf_addr + md_ftrace_buf_current;
  535. memcpy(addr, buf, size);
  536. md_ftrace_buf_current += size;
  537. }
  538. static void md_trace_oops_enter(void *unused, bool *enter_check)
  539. {
  540. if (!minidump_ftrace_in_oops) {
  541. minidump_ftrace_in_oops = true;
  542. *enter_check = false;
  543. } else {
  544. *enter_check = true;
  545. }
  546. }
  547. static void md_trace_oops_exit(void *unused, bool *exit_check)
  548. {
  549. minidump_ftrace_in_oops = false;
  550. }
  551. static void md_update_trace_fmt(void *unused, bool *format_check)
  552. {
  553. *format_check = false;
  554. }
  555. static void md_buf_size_check(void *unused, unsigned long buffer_size,
  556. bool *size_check)
  557. {
  558. if (!minidump_ftrace_dump) {
  559. *size_check = true;
  560. return;
  561. }
  562. if (buffer_size > (SZ_256K + PAGE_SIZE)) {
  563. pr_err("Skip md ftrace buffer dump for: %#lx\n", buffer_size);
  564. minidump_ftrace_dump = false;
  565. *size_check = true;
  566. }
  567. }
  568. static void md_dump_trace_buf(void *unused, struct trace_seq *trace_buf,
  569. bool *printk_check)
  570. {
  571. if (minidump_ftrace_in_oops && minidump_ftrace_dump) {
  572. minidump_add_trace_event(trace_buf->buffer,
  573. trace_buf->seq.len);
  574. *printk_check = false;
  575. }
  576. }
  577. static void md_register_trace_buf(void)
  578. {
  579. struct md_region md_entry;
  580. void *buffer_start;
  581. buffer_start = kzalloc(MD_FTRACE_BUF_SIZE, GFP_KERNEL);
  582. if (!buffer_start)
  583. return;
  584. strscpy(md_entry.name, "KFTRACE", sizeof(md_entry.name));
  585. md_entry.virt_addr = (uintptr_t)buffer_start;
  586. md_entry.phys_addr = virt_to_phys(buffer_start);
  587. md_entry.size = MD_FTRACE_BUF_SIZE;
  588. if (msm_minidump_add_region(&md_entry) < 0)
  589. pr_err("Failed to add ftrace buffer entry in Minidump\n");
  590. register_trace_android_vh_ftrace_oops_enter(md_trace_oops_enter,
  591. NULL);
  592. register_trace_android_vh_ftrace_oops_exit(md_trace_oops_exit,
  593. NULL);
  594. register_trace_android_vh_ftrace_size_check(md_buf_size_check,
  595. NULL);
  596. register_trace_android_vh_ftrace_format_check(md_update_trace_fmt,
  597. NULL);
  598. register_trace_android_vh_ftrace_dump_buffer(md_dump_trace_buf,
  599. NULL);
  600. /* Complete registration before adding enteries */
  601. smp_mb();
  602. WRITE_ONCE(md_ftrace_buf_addr, buffer_start);
  603. }
  604. #endif
  605. #ifdef CONFIG_QCOM_MINIDUMP_PANIC_DUMP
  606. static void md_dump_align(void)
  607. {
  608. int tab_offset = md_align_offset;
  609. while (tab_offset--)
  610. seq_buf_printf(md_runq_seq_buf, " | ");
  611. seq_buf_printf(md_runq_seq_buf, " |--");
  612. }
  613. static void md_dump_task_info(struct task_struct *task, char *status,
  614. struct task_struct *curr)
  615. {
  616. struct sched_entity *se;
  617. md_dump_align();
  618. if (!task) {
  619. seq_buf_printf(md_runq_seq_buf, "%s : None(0)\n", status);
  620. return;
  621. }
  622. se = &task->se;
  623. if (task == curr) {
  624. seq_buf_printf(md_runq_seq_buf,
  625. "[status: curr] pid: %d preempt: %#x\n",
  626. task_pid_nr(task),
  627. task->thread_info.preempt_count);
  628. return;
  629. }
  630. seq_buf_printf(md_runq_seq_buf,
  631. "[status: %s] pid: %d\n",
  632. status, task_pid_nr(task));
  633. }
  634. static void md_dump_cfs_rq(struct cfs_rq *cfs, struct task_struct *curr);
  635. static void md_dump_cgroup_state(char *status, struct sched_entity *se_p,
  636. struct task_struct *curr)
  637. {
  638. struct task_struct *task;
  639. struct cfs_rq *my_q = NULL;
  640. unsigned int nr_running;
  641. if (!se_p) {
  642. md_dump_task_info(NULL, status, NULL);
  643. return;
  644. }
  645. #ifdef CONFIG_FAIR_GROUP_SCHED
  646. my_q = se_p->my_q;
  647. #endif
  648. if (!my_q) {
  649. task = container_of(se_p, struct task_struct, se);
  650. md_dump_task_info(task, status, curr);
  651. return;
  652. }
  653. nr_running = my_q->nr_running;
  654. md_dump_align();
  655. seq_buf_printf(md_runq_seq_buf, "%s: %d process is grouping\n",
  656. status, nr_running);
  657. md_align_offset++;
  658. md_dump_cfs_rq(my_q, curr);
  659. md_align_offset--;
  660. }
  661. static void md_dump_cfs_node_func(struct rb_node *node,
  662. struct task_struct *curr)
  663. {
  664. struct sched_entity *se_p = container_of(node, struct sched_entity,
  665. run_node);
  666. md_dump_cgroup_state("pend", se_p, curr);
  667. }
  668. static void md_rb_walk_cfs(struct rb_root_cached *rb_root_cached_p,
  669. struct task_struct *curr)
  670. {
  671. int max_walk = 200; /* Bail out, in case of loop */
  672. struct rb_node *leftmost = rb_root_cached_p->rb_leftmost;
  673. struct rb_root *root = &rb_root_cached_p->rb_root;
  674. struct rb_node *rb_node = rb_first(root);
  675. if (!leftmost)
  676. return;
  677. while (rb_node && max_walk--) {
  678. md_dump_cfs_node_func(rb_node, curr);
  679. rb_node = rb_next(rb_node);
  680. }
  681. }
  682. static void md_dump_cfs_rq(struct cfs_rq *cfs, struct task_struct *curr)
  683. {
  684. struct rb_root_cached *rb_root_cached_p = &cfs->tasks_timeline;
  685. md_dump_cgroup_state("curr", cfs->curr, curr);
  686. md_dump_cgroup_state("next", cfs->next, curr);
  687. md_dump_cgroup_state("last", cfs->last, curr);
  688. md_dump_cgroup_state("skip", cfs->skip, curr);
  689. md_rb_walk_cfs(rb_root_cached_p, curr);
  690. }
  691. static void md_dump_rt_rq(struct rt_rq *rt_rq, struct task_struct *curr)
  692. {
  693. struct rt_prio_array *array = &rt_rq->active;
  694. struct sched_rt_entity *rt_se;
  695. int idx;
  696. /* Lifted most of the below code from dump_throttled_rt_tasks() */
  697. if (bitmap_empty(array->bitmap, MAX_RT_PRIO))
  698. return;
  699. idx = sched_find_first_bit(array->bitmap);
  700. while (idx < MAX_RT_PRIO) {
  701. list_for_each_entry(rt_se, array->queue + idx, run_list) {
  702. struct task_struct *p;
  703. #ifdef CONFIG_RT_GROUP_SCHED
  704. if (rt_se->my_q)
  705. continue;
  706. #endif
  707. p = container_of(rt_se, struct task_struct, rt);
  708. md_dump_task_info(p, "pend", curr);
  709. }
  710. idx = find_next_bit(array->bitmap, MAX_RT_PRIO, idx + 1);
  711. }
  712. }
  713. static const char * const task_state_array[] = {
  714. "R", /* 0x00 */
  715. "S", /* 0x01 */
  716. "D", /* 0x02 */
  717. "T", /* 0x04 */
  718. "t", /* 0x08 */
  719. "X", /* 0x10 */
  720. "Z", /* 0x20 */
  721. "P", /* 0x40 */
  722. "I", /* 0x80 */
  723. };
  724. /* In line with task_state_index from fs/proc/array.c */
  725. static inline unsigned int md_task_state_index(struct task_struct *tsk)
  726. {
  727. unsigned int tsk_state = READ_ONCE(tsk->__state);
  728. unsigned int state = (tsk_state | tsk->exit_state) & TASK_REPORT;
  729. if (tsk_state == TASK_IDLE)
  730. state = TASK_REPORT_IDLE;
  731. return fls(state);
  732. }
  733. /* In line with get_task_state from fs/proc/array.c */
  734. static inline const char *md_get_task_state(struct task_struct *tsk)
  735. {
  736. return task_state_array[md_task_state_index(tsk)];
  737. }
  738. static void md_dump_next_event(void)
  739. {
  740. int cpu;
  741. struct tick_device *device_dump;
  742. struct clock_event_device *event_dev;
  743. device_dump = DEBUG_SYMBOL_LOOKUP(tick_cpu_device);
  744. if (!device_dump)
  745. return;
  746. for_each_possible_cpu(cpu) {
  747. event_dev = per_cpu(device_dump->evtdev, cpu);
  748. if (event_dev)
  749. pr_emerg("CPU%d next event is %ld\n", cpu,
  750. event_dev->next_event);
  751. else
  752. pr_emerg("CPU%d next event is not available\n", cpu);
  753. }
  754. }
  755. static void md_dump_runqueues(void)
  756. {
  757. int cpu;
  758. struct rq *rq;
  759. struct rt_rq *rt;
  760. struct cfs_rq *cfs;
  761. struct task_struct *p, *t;
  762. #if IS_ENABLED(CONFIG_SCHED_WALT)
  763. struct walt_task_struct *wts;
  764. #endif
  765. if (!md_runq_seq_buf)
  766. return;
  767. for_each_possible_cpu(cpu) {
  768. rq = cpu_rq(cpu);
  769. rt = &rq->rt;
  770. cfs = &rq->cfs;
  771. seq_buf_printf(md_runq_seq_buf,
  772. "CPU%d has %d process, current is pid %d\n",
  773. cpu, rq->nr_running, cpu_curr(cpu)->pid);
  774. seq_buf_printf(md_runq_seq_buf,
  775. "CFS has %d process\n",
  776. cfs->nr_running);
  777. md_dump_cfs_rq(cfs, cpu_curr(cpu));
  778. seq_buf_printf(md_runq_seq_buf,
  779. "RT has %d process\n",
  780. rt->rt_nr_running);
  781. md_dump_rt_rq(rt, cpu_curr(cpu));
  782. seq_buf_printf(md_runq_seq_buf, "\n");
  783. }
  784. seq_buf_printf(md_runq_seq_buf, "%-15s", "Task name");
  785. seq_buf_printf(md_runq_seq_buf, "%*s", 6, "PID");
  786. seq_buf_printf(md_runq_seq_buf, "%*s", 16, "Exec_started_at");
  787. seq_buf_printf(md_runq_seq_buf, "%*s", 16, "Last_queued_at");
  788. seq_buf_printf(md_runq_seq_buf, "%*s", 16, "Total_wait_time");
  789. seq_buf_printf(md_runq_seq_buf, "%*s", 12, "Exec_times");
  790. seq_buf_printf(md_runq_seq_buf, "%*s", 4, "CPU");
  791. seq_buf_printf(md_runq_seq_buf, "%*s", 5, "Prio");
  792. seq_buf_printf(md_runq_seq_buf, "%*s", 6, "State");
  793. #if IS_ENABLED(CONFIG_SCHED_WALT)
  794. seq_buf_printf(md_runq_seq_buf, "%*s", 17, "Last_enqueued_ts");
  795. seq_buf_printf(md_runq_seq_buf, "%*s", 16, "Last_sleep_ts");
  796. #endif
  797. seq_buf_printf(md_runq_seq_buf, "\n");
  798. for_each_process_thread(p, t) {
  799. #ifndef CONFIG_MINIDUMP_ALL_TASK_INFO
  800. if (READ_ONCE(t->__state))
  801. continue;
  802. #endif
  803. seq_buf_printf(md_runq_seq_buf, "%-15s", t->comm);
  804. seq_buf_printf(md_runq_seq_buf, "%6d", t->pid);
  805. seq_buf_printf(md_runq_seq_buf, "%16lld", t->sched_info.last_arrival);
  806. seq_buf_printf(md_runq_seq_buf, "%16lld", t->sched_info.last_queued);
  807. seq_buf_printf(md_runq_seq_buf, "%16lld", t->sched_info.run_delay);
  808. seq_buf_printf(md_runq_seq_buf, "%12ld", t->sched_info.pcount);
  809. seq_buf_printf(md_runq_seq_buf, "%4d", t->on_cpu);
  810. seq_buf_printf(md_runq_seq_buf, "%5d", t->prio);
  811. seq_buf_printf(md_runq_seq_buf, "%*s", 6, md_get_task_state(t));
  812. #if IS_ENABLED(CONFIG_SCHED_WALT)
  813. wts = (struct walt_task_struct *) t->android_vendor_data1;
  814. seq_buf_printf(md_runq_seq_buf, "%17ld", wts->last_enqueued_ts);
  815. seq_buf_printf(md_runq_seq_buf, "%16ld", wts->last_sleep_ts);
  816. #endif
  817. seq_buf_printf(md_runq_seq_buf, "\n");
  818. }
  819. }
  820. #ifdef CONFIG_QCOM_MINIDUMP_PANIC_CPU_CONTEXT
  821. /*
  822. * dump a block of kernel memory from around the given address.
  823. * Bulk of the code is lifted from arch/arm64/kernel/proccess.c.
  824. */
  825. static void md_dump_data(unsigned long addr, int nbytes, const char *name)
  826. {
  827. int i, j;
  828. int nlines;
  829. u32 *p;
  830. /*
  831. * don't attempt to dump non-kernel addresses or
  832. * values that are probably just small negative numbers
  833. */
  834. if (addr < PAGE_OFFSET || addr > -256UL)
  835. return;
  836. seq_buf_printf(md_cntxt_seq_buf, "\n%s: %#lx:\n", name, addr);
  837. /*
  838. * round address down to a 32 bit boundary
  839. * and always dump a multiple of 32 bytes
  840. */
  841. p = (u32 *)(addr & ~(sizeof(u32) - 1));
  842. nbytes += (addr & (sizeof(u32) - 1));
  843. nlines = (nbytes + 31) / 32;
  844. for (i = 0; i < nlines; i++) {
  845. /*
  846. * just display low 16 bits of address to keep
  847. * each line of the dump < 80 characters
  848. */
  849. seq_buf_printf(md_cntxt_seq_buf, "%04lx ",
  850. (unsigned long)p & 0xffff);
  851. for (j = 0; j < 8; j++) {
  852. u32 data = 0;
  853. if (get_kernel_nofault(data, p))
  854. seq_buf_printf(md_cntxt_seq_buf, " ********");
  855. else
  856. seq_buf_printf(md_cntxt_seq_buf, " %08x", data);
  857. ++p;
  858. }
  859. seq_buf_printf(md_cntxt_seq_buf, "\n");
  860. }
  861. }
  862. static void md_reg_context_data(struct pt_regs *regs)
  863. {
  864. int nbytes = 128;
  865. if (user_mode(regs) || !regs->pc)
  866. return;
  867. md_dump_data(regs->pc - nbytes, nbytes * 2, "PC");
  868. md_dump_data(regs->regs[30] - nbytes, nbytes * 2, "LR");
  869. md_dump_data(regs->sp - nbytes, nbytes * 2, "SP");
  870. }
  871. static inline void md_dump_panic_regs(void)
  872. {
  873. struct pt_regs regs;
  874. u64 tmp1, tmp2;
  875. /* Lifted from crash_setup_regs() */
  876. __asm__ __volatile__ (
  877. "stp x0, x1, [%2, #16 * 0]\n"
  878. "stp x2, x3, [%2, #16 * 1]\n"
  879. "stp x4, x5, [%2, #16 * 2]\n"
  880. "stp x6, x7, [%2, #16 * 3]\n"
  881. "stp x8, x9, [%2, #16 * 4]\n"
  882. "stp x10, x11, [%2, #16 * 5]\n"
  883. "stp x12, x13, [%2, #16 * 6]\n"
  884. "stp x14, x15, [%2, #16 * 7]\n"
  885. "stp x16, x17, [%2, #16 * 8]\n"
  886. "stp x18, x19, [%2, #16 * 9]\n"
  887. "stp x20, x21, [%2, #16 * 10]\n"
  888. "stp x22, x23, [%2, #16 * 11]\n"
  889. "stp x24, x25, [%2, #16 * 12]\n"
  890. "stp x26, x27, [%2, #16 * 13]\n"
  891. "stp x28, x29, [%2, #16 * 14]\n"
  892. "mov %0, sp\n"
  893. "stp x30, %0, [%2, #16 * 15]\n"
  894. "/* faked current PSTATE */\n"
  895. "mrs %0, CurrentEL\n"
  896. "mrs %1, SPSEL\n"
  897. "orr %0, %0, %1\n"
  898. "mrs %1, DAIF\n"
  899. "orr %0, %0, %1\n"
  900. "mrs %1, NZCV\n"
  901. "orr %0, %0, %1\n"
  902. /* pc */
  903. "adr %1, 1f\n"
  904. "1:\n"
  905. "stp %1, %0, [%2, #16 * 16]\n"
  906. : "=&r" (tmp1), "=&r" (tmp2)
  907. : "r" (&regs)
  908. : "memory"
  909. );
  910. seq_buf_printf(md_cntxt_seq_buf, "PANIC CPU : %d\n",
  911. raw_smp_processor_id());
  912. md_reg_context_data(&regs);
  913. }
  914. static void md_dump_other_cpus_context(void)
  915. {
  916. int cpu;
  917. struct pt_regs regs;
  918. for_each_possible_cpu(cpu) {
  919. regs = per_cpu(regs_before_stop, cpu);
  920. seq_buf_printf(md_cntxt_seq_buf, "\nSTOPPED CPU : %d\n", cpu);
  921. md_reg_context_data(&regs);
  922. }
  923. }
  924. static int md_die_context_notify(struct notifier_block *self,
  925. unsigned long val, void *data)
  926. {
  927. struct die_args *args = (struct die_args *)data;
  928. if (md_in_oops_handler)
  929. return NOTIFY_DONE;
  930. md_in_oops_handler = true;
  931. if (!md_cntxt_seq_buf) {
  932. md_in_oops_handler = false;
  933. return NOTIFY_DONE;
  934. }
  935. die_cpu = raw_smp_processor_id();
  936. seq_buf_printf(md_cntxt_seq_buf, "\nDIE CPU : %d\n", die_cpu);
  937. md_reg_context_data(args->regs);
  938. md_in_oops_handler = false;
  939. return NOTIFY_DONE;
  940. }
  941. static struct notifier_block md_die_context_nb = {
  942. .notifier_call = md_die_context_notify,
  943. .priority = INT_MAX - 2, /* < msm watchdog die notifier */
  944. };
  945. static void md_ipi_stop(void *unused, struct pt_regs *regs)
  946. {
  947. unsigned int cpu = smp_processor_id();
  948. per_cpu(regs_before_stop, cpu) = *regs;
  949. }
  950. #endif
  951. void md_dump_process(void)
  952. {
  953. if (md_in_oops_handler)
  954. return;
  955. if (!atomic_add_unless(&md_handle_done, 1, 1))
  956. return;
  957. md_in_oops_handler = true;
  958. #ifdef CONFIG_QCOM_MINIDUMP_PANIC_CPU_CONTEXT
  959. if (!md_cntxt_seq_buf)
  960. goto dump_rq;
  961. if (raw_smp_processor_id() != die_cpu)
  962. md_dump_panic_regs();
  963. md_dump_other_cpus_context();
  964. dump_rq:
  965. #endif
  966. md_dump_next_event();
  967. md_dump_runqueues();
  968. #ifdef CONFIG_QCOM_MINIDUMP_PANIC_MEMORY_INFO
  969. if (md_meminfo_seq_buf)
  970. md_dump_meminfo(md_meminfo_seq_buf);
  971. #ifdef CONFIG_SLUB_DEBUG
  972. if (md_slabinfo_seq_buf)
  973. md_dump_slabinfo(md_slabinfo_seq_buf);
  974. #endif
  975. #ifdef CONFIG_PAGE_OWNER
  976. if (md_pageowner_dump_addr)
  977. md_dump_pageowner(md_pageowner_dump_addr, md_pageowner_dump_size);
  978. #endif
  979. #ifdef CONFIG_SLUB_DEBUG
  980. if (md_slabowner_dump_addr)
  981. md_dump_slabowner(md_slabowner_dump_addr, md_slabowner_dump_size);
  982. #endif
  983. if (md_dma_buf_info_addr)
  984. md_dma_buf_info(md_dma_buf_info_addr, md_dma_buf_info_size);
  985. if (md_dma_buf_procs_addr)
  986. md_dma_buf_procs(md_dma_buf_procs_addr, md_dma_buf_procs_size);
  987. #endif
  988. md_in_oops_handler = false;
  989. }
  990. EXPORT_SYMBOL(md_dump_process);
  991. static int md_panic_handler(struct notifier_block *this,
  992. unsigned long event, void *ptr)
  993. {
  994. md_dump_process();
  995. return NOTIFY_DONE;
  996. }
  997. static struct notifier_block md_panic_blk = {
  998. .notifier_call = md_panic_handler,
  999. .priority = INT_MAX - 2, /* < msm watchdog panic notifier */
  1000. };
  1001. static int md_register_minidump_entry(char *name, u64 virt_addr,
  1002. u64 phys_addr, u64 size)
  1003. {
  1004. struct md_region md_entry;
  1005. int ret;
  1006. strscpy(md_entry.name, name, sizeof(md_entry.name));
  1007. md_entry.virt_addr = virt_addr;
  1008. md_entry.phys_addr = phys_addr;
  1009. md_entry.size = size;
  1010. ret = msm_minidump_add_region(&md_entry);
  1011. if (ret < 0)
  1012. pr_err("Failed to add %s entry in Minidump\n", name);
  1013. return ret;
  1014. }
  1015. static int md_register_panic_entries(int num_pages, char *name,
  1016. struct seq_buf **global_buf)
  1017. {
  1018. char *buf;
  1019. struct seq_buf *seq_buf_p;
  1020. int ret;
  1021. buf = kzalloc(num_pages * PAGE_SIZE, GFP_KERNEL);
  1022. if (!buf)
  1023. return -EINVAL;
  1024. seq_buf_p = kzalloc(sizeof(*seq_buf_p), GFP_KERNEL);
  1025. if (!seq_buf_p) {
  1026. ret = -EINVAL;
  1027. goto err_seq_buf;
  1028. }
  1029. ret = md_register_minidump_entry(name, (uintptr_t)buf,
  1030. virt_to_phys(buf),
  1031. num_pages * PAGE_SIZE);
  1032. if (ret < 0)
  1033. goto err_entry_reg;
  1034. seq_buf_init(seq_buf_p, buf, num_pages * PAGE_SIZE);
  1035. /* Complete registration before populating data */
  1036. smp_mb();
  1037. WRITE_ONCE(*global_buf, seq_buf_p);
  1038. return 0;
  1039. err_entry_reg:
  1040. kfree(seq_buf_p);
  1041. err_seq_buf:
  1042. kfree(buf);
  1043. return ret;
  1044. }
  1045. static void md_register_panic_data(void)
  1046. {
  1047. #ifdef CONFIG_QCOM_MINIDUMP_PANIC_MEMORY_INFO
  1048. struct dentry *minidump_dir = NULL;
  1049. int ret;
  1050. ret = md_minidump_memory_init();
  1051. if (ret) {
  1052. pr_err("Failed to look up all minidump memory symbols, rc: %d\n", ret);
  1053. return;
  1054. }
  1055. md_register_panic_entries(MD_MEMINFO_PAGES, "MEMINFO",
  1056. &md_meminfo_seq_buf);
  1057. #ifdef CONFIG_SLUB_DEBUG
  1058. md_register_panic_entries(MD_SLABINFO_PAGES, "SLABINFO",
  1059. &md_slabinfo_seq_buf);
  1060. #endif
  1061. if (!minidump_dir)
  1062. minidump_dir = debugfs_create_dir("minidump", NULL);
  1063. #ifdef CONFIG_PAGE_OWNER
  1064. if (is_page_owner_enabled()) {
  1065. md_register_memory_dump(md_pageowner_dump_size, "PAGEOWNER");
  1066. md_debugfs_pageowner(minidump_dir);
  1067. }
  1068. #endif
  1069. #ifdef CONFIG_SLUB_DEBUG
  1070. if (is_slub_debug_enabled()) {
  1071. md_register_memory_dump(md_slabowner_dump_size, "SLABOWNER");
  1072. md_debugfs_slabowner(minidump_dir);
  1073. }
  1074. #endif
  1075. md_register_memory_dump(md_dma_buf_info_size, "DMA_INFO");
  1076. md_debugfs_dmabufinfo(minidump_dir);
  1077. md_register_memory_dump(md_dma_buf_procs_size, "DMA_PROC");
  1078. md_debugfs_dmabufprocs(minidump_dir);
  1079. #endif
  1080. #ifdef CONFIG_QCOM_MINIDUMP_PANIC_CPU_CONTEXT
  1081. md_register_panic_entries(MD_CPU_CNTXT_PAGES, "KCNTXT",
  1082. &md_cntxt_seq_buf);
  1083. register_trace_android_vh_ipi_stop(md_ipi_stop, NULL);
  1084. #endif
  1085. md_register_panic_entries(MD_RUNQUEUE_PAGES, "KRUNQUEUE",
  1086. &md_runq_seq_buf);
  1087. }
  1088. static int register_vmap_mem(const char *name, void *virual_addr, size_t dump_len)
  1089. {
  1090. int to_dump;
  1091. u64 phys_addr;
  1092. char entry_name[12];
  1093. void *dump_addr = virual_addr;
  1094. int i = 0;
  1095. while (dump_len) {
  1096. to_dump = min(dump_len, PAGE_SIZE - offset_in_page(dump_addr));
  1097. phys_addr = page_to_phys(vmalloc_to_page((const void *)dump_addr));
  1098. snprintf(entry_name, sizeof(entry_name), "%d_%s", i, name);
  1099. md_register_minidump_entry(entry_name, (u64)dump_addr, phys_addr, to_dump);
  1100. dump_addr += to_dump;
  1101. dump_len -= to_dump;
  1102. i++;
  1103. }
  1104. return 0;
  1105. }
  1106. struct module_sect_attr {
  1107. struct bin_attribute battr;
  1108. unsigned long address;
  1109. };
  1110. struct module_sect_attrs {
  1111. struct attribute_group grp;
  1112. unsigned int nsections;
  1113. struct module_sect_attr attrs[];
  1114. };
  1115. static int md_module_process(struct module *mod)
  1116. {
  1117. int i;
  1118. bool is_key_module = false;
  1119. unsigned long sec_addr, base_addr;
  1120. unsigned long dump_start, dump_end;
  1121. for (i = 0; i < n_modump; i++) {
  1122. if (strcmp(key_modules[i], mod->name) == 0)
  1123. is_key_module = true;
  1124. }
  1125. if (md_mod_info_seq_buf) {
  1126. base_addr = (unsigned long)mod->core_layout.base;
  1127. seq_buf_printf(md_mod_info_seq_buf, "name: %s, base: %lx",
  1128. mod->name, base_addr);
  1129. if (is_key_module) {
  1130. dump_start = base_addr +
  1131. mod->core_layout.ro_after_init_size;
  1132. dump_end = base_addr + mod->core_layout.size;
  1133. if (((dump_end - dump_start) / PAGE_SIZE) <
  1134. msm_minidump_get_available_region()) {
  1135. for (i = 0; i < mod->sect_attrs->nsections ; i++) {
  1136. sec_addr = mod->sect_attrs->attrs[i].address;
  1137. if (sec_addr >= dump_start && sec_addr < dump_end) {
  1138. seq_buf_printf(md_mod_info_seq_buf, ", %s: %lx",
  1139. mod->sect_attrs->attrs[i].battr.attr.name,
  1140. sec_addr);
  1141. }
  1142. }
  1143. register_vmap_mem(mod->name, (void *)dump_start,
  1144. (dump_end - dump_start));
  1145. } else
  1146. pr_err("Failed to dump module %s\n", mod->name);
  1147. }
  1148. seq_buf_printf(md_mod_info_seq_buf, "\n");
  1149. }
  1150. return 0;
  1151. }
  1152. static int md_module_notify(struct notifier_block *self,
  1153. unsigned long val, void *data)
  1154. {
  1155. struct module *mod = data;
  1156. spin_lock(&md_modules_lock);
  1157. if (mod->state == MODULE_STATE_LIVE)
  1158. md_module_process(mod);
  1159. spin_unlock(&md_modules_lock);
  1160. return 0;
  1161. }
  1162. static struct notifier_block md_module_nb = {
  1163. .notifier_call = md_module_notify,
  1164. };
  1165. static void md_register_module_data(void)
  1166. {
  1167. int ret;
  1168. struct module *module;
  1169. struct list_head *module_list;
  1170. ret = md_register_panic_entries(MD_MODULE_PAGES, "KMODULES",
  1171. &md_mod_info_seq_buf);
  1172. if (ret) {
  1173. pr_err("Failed to register minidump module buffer\n");
  1174. return;
  1175. }
  1176. seq_buf_printf(md_mod_info_seq_buf, "=== MODULE INFO ===\n");
  1177. ret = register_module_notifier(&md_module_nb);
  1178. if (ret) {
  1179. pr_err("Failed to register minidump module notifier\n");
  1180. return;
  1181. }
  1182. module_list = DEBUG_SYMBOL_LOOKUP(modules);
  1183. if (IS_ERR_OR_NULL(module_list))
  1184. return;
  1185. preempt_disable();
  1186. list_for_each_entry_rcu(module, module_list, list) {
  1187. if (module != THIS_MODULE)
  1188. md_module_process(module);
  1189. }
  1190. preempt_enable();
  1191. }
  1192. struct freq_log {
  1193. uint64_t ktime;
  1194. uint64_t freq;
  1195. };
  1196. struct freq_hist {
  1197. uint32_t idx;
  1198. struct freq_log log[FREQ_LOG_MAX];
  1199. };
  1200. static int max_cluster;
  1201. static struct freq_hist *cpuclk_log;
  1202. static void log_cpu_freq(void *unused,
  1203. struct cpufreq_policy *policy,
  1204. unsigned int *target_freq,
  1205. unsigned int old_target_freq)
  1206. {
  1207. uint32_t index;
  1208. int cluster = topology_cluster_id(policy->cpu);
  1209. if (cluster > max_cluster)
  1210. return;
  1211. index = cpuclk_log[cluster].idx;
  1212. cpuclk_log[cluster].log[index].ktime = sched_clock();
  1213. cpuclk_log[cluster].log[index].freq = *target_freq;
  1214. cpuclk_log[cluster].idx = (index + 1) % FREQ_LOG_MAX;
  1215. }
  1216. static void register_cpufreq_log(void)
  1217. {
  1218. int cpu;
  1219. struct md_region md_entry;
  1220. size_t freq_hist_sz;
  1221. for_each_possible_cpu(cpu) {
  1222. if (topology_cluster_id(cpu) > max_cluster)
  1223. max_cluster = topology_cluster_id(cpu);
  1224. }
  1225. freq_hist_sz = sizeof(struct freq_hist) * (max_cluster + 1);
  1226. cpuclk_log = kzalloc(freq_hist_sz, GFP_KERNEL);
  1227. strscpy(md_entry.name, "FREQ_LOG", sizeof(md_entry.name));
  1228. md_entry.virt_addr = (uintptr_t)cpuclk_log;
  1229. md_entry.phys_addr = virt_to_phys(cpuclk_log);
  1230. md_entry.size = freq_hist_sz;
  1231. if (msm_minidump_add_region(&md_entry) < 0)
  1232. pr_err("Failed to add pmsg in Minidump\n");
  1233. register_trace_android_vh_cpufreq_resolve_freq(log_cpu_freq, NULL);
  1234. register_trace_android_vh_cpufreq_fast_switch(log_cpu_freq, NULL);
  1235. register_trace_android_vh_cpufreq_target(log_cpu_freq, NULL);
  1236. }
  1237. #endif
  1238. #ifdef CONFIG_QCOM_MINIDUMP_PSTORE
  1239. static void register_pstore_info(void)
  1240. {
  1241. int ret;
  1242. struct device_node *node;
  1243. struct resource resource;
  1244. struct reserved_mem *rmem = NULL;
  1245. unsigned int size;
  1246. phys_addr_t paddr;
  1247. unsigned long total_size;
  1248. struct md_region md_entry;
  1249. node = of_find_compatible_node(NULL, NULL, "ramoops");
  1250. if (IS_ERR_OR_NULL(node)) {
  1251. pr_err("Failed to get pstore node\n");
  1252. return;
  1253. }
  1254. ret = of_address_to_resource(node, 0, &resource);
  1255. if (ret) {
  1256. rmem = of_reserved_mem_lookup(node);
  1257. if (rmem) {
  1258. paddr = rmem->base;
  1259. total_size = rmem->size;
  1260. } else {
  1261. pr_err("Failed to get pstore mem\n");
  1262. return;
  1263. }
  1264. } else {
  1265. paddr = resource.start;
  1266. total_size = resource_size(&resource);
  1267. }
  1268. ret = of_property_read_u32(node, "record-size", &size);
  1269. if (!ret && size > 0) {
  1270. strscpy(md_entry.name, "KDMESG", sizeof(md_entry.name));
  1271. md_entry.virt_addr = (uintptr_t)phys_to_virt(paddr);
  1272. md_entry.phys_addr = paddr;
  1273. md_entry.size = size;
  1274. if (msm_minidump_add_region(&md_entry) < 0)
  1275. pr_err("Failed to add dmesg in Minidump\n");
  1276. paddr += size;
  1277. }
  1278. ret = of_property_read_u32(node, "console-size", &size);
  1279. if (!ret && size > 0) {
  1280. strscpy(md_entry.name, "KCONSOLE", sizeof(md_entry.name));
  1281. md_entry.virt_addr = (uintptr_t)phys_to_virt(paddr);
  1282. md_entry.phys_addr = paddr;
  1283. md_entry.size = size;
  1284. if (msm_minidump_add_region(&md_entry) < 0)
  1285. pr_err("Failed to add console in Minidump\n");
  1286. paddr += size;
  1287. }
  1288. ret = of_property_read_u32(node, "ftrace-size", &size);
  1289. if (!ret && size > 0) {
  1290. strscpy(md_entry.name, "KFTRACE", sizeof(md_entry.name));
  1291. md_entry.virt_addr = (uintptr_t)phys_to_virt(paddr);
  1292. md_entry.phys_addr = paddr;
  1293. md_entry.size = size;
  1294. if (msm_minidump_add_region(&md_entry) < 0)
  1295. pr_err("Failed to add ftrace in Minidump\n");
  1296. paddr += size;
  1297. }
  1298. ret = of_property_read_u32(node, "pmsg-size", &size);
  1299. if (!ret && size > 0) {
  1300. strscpy(md_entry.name, "KPMSG", sizeof(md_entry.name));
  1301. md_entry.virt_addr = (uintptr_t)phys_to_virt(paddr);
  1302. md_entry.phys_addr = paddr;
  1303. md_entry.size = size;
  1304. if (msm_minidump_add_region(&md_entry) < 0)
  1305. pr_err("Failed to add pmsg in Minidump\n");
  1306. paddr += size;
  1307. }
  1308. }
  1309. #endif
  1310. static inline int __msm_minidump_log_init(void)
  1311. {
  1312. register_kernel_sections();
  1313. is_vmap_stack = IS_ENABLED(CONFIG_VMAP_STACK);
  1314. register_irq_stack();
  1315. #ifdef CONFIG_QCOM_DYN_MINIDUMP_STACK
  1316. register_current_stack();
  1317. register_suspend_context();
  1318. #endif
  1319. #ifdef CONFIG_QCOM_MINIDUMP_PSTORE
  1320. register_pstore_info();
  1321. #endif
  1322. #ifdef CONFIG_QCOM_MINIDUMP_FTRACE
  1323. md_register_trace_buf();
  1324. #endif
  1325. #ifdef CONFIG_QCOM_MINIDUMP_PANIC_DUMP
  1326. register_cpufreq_log();
  1327. md_register_module_data();
  1328. md_register_panic_data();
  1329. atomic_notifier_chain_register(&panic_notifier_list, &md_panic_blk);
  1330. #ifdef CONFIG_QCOM_MINIDUMP_PANIC_CPU_CONTEXT
  1331. register_die_notifier(&md_die_context_nb);
  1332. #endif
  1333. #endif
  1334. return 0;
  1335. }
  1336. int msm_minidump_log_init(void)
  1337. {
  1338. if (!sec_debug_is_enabled())
  1339. return 0;
  1340. return __msm_minidump_log_init();
  1341. }