ipc_logging.c 28 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * Copyright (c) 2012-2023 Qualcomm Innovation Center, Inc. All rights reserved.
  4. */
  5. #include <asm/arch_timer.h>
  6. #include <linux/slab.h>
  7. #include <linux/uaccess.h>
  8. #include <linux/module.h>
  9. #include <linux/fs.h>
  10. #include <linux/kernel.h>
  11. #include <linux/errno.h>
  12. #include <linux/jiffies.h>
  13. #include <linux/debugfs.h>
  14. #include <linux/io.h>
  15. #include <linux/idr.h>
  16. #include <linux/string.h>
  17. #include <linux/sched.h>
  18. #include <linux/wait.h>
  19. #include <linux/delay.h>
  20. #include <linux/completion.h>
  21. #include <linux/sched/clock.h>
  22. #include <linux/ipc_logging.h>
  23. #include <soc/qcom/minidump.h>
  24. #include <linux/samsung/debug/sec_debug.h>
  25. #include "ipc_logging_private.h"
  26. #define LOG_PAGE_DATA_SIZE sizeof(((struct ipc_log_page *)0)->data)
  27. #define LOG_PAGE_FLAG (1 << 31)
  28. #define MAX_MINIDUMP_BUFFERS CONFIG_IPC_LOG_MINIDUMP_BUFFERS
  29. /*16th bit is used for minidump feature*/
  30. #define FEATURE_MASK 0x10000
  31. static int minidump_buf_cnt;
  32. static LIST_HEAD(ipc_log_context_list);
  33. static DEFINE_RWLOCK(context_list_lock_lha1);
  34. static void *get_deserialization_func(struct ipc_log_context *ilctxt,
  35. int type);
  36. static void *debug_level_dummy_ctx;
  37. static struct ipc_log_page *get_first_page(struct ipc_log_context *ilctxt)
  38. {
  39. struct ipc_log_page_header *p_pghdr;
  40. struct ipc_log_page *pg = NULL;
  41. if (!ilctxt)
  42. return NULL;
  43. p_pghdr = list_first_entry(&ilctxt->page_list,
  44. struct ipc_log_page_header, list);
  45. pg = container_of(p_pghdr, struct ipc_log_page, hdr);
  46. return pg;
  47. }
  48. /**
  49. * is_nd_read_empty - Returns true if no data is available to read in log
  50. *
  51. * @ilctxt: logging context
  52. * @returns: > 1 if context is empty; 0 if not empty; <0 for failure
  53. *
  54. * This is for the debugfs read pointer which allows for a non-destructive read.
  55. * There may still be data in the log, but it may have already been read.
  56. */
  57. static int is_nd_read_empty(struct ipc_log_context *ilctxt)
  58. {
  59. if (!ilctxt)
  60. return -EINVAL;
  61. return ((ilctxt->nd_read_page == ilctxt->write_page) &&
  62. (ilctxt->nd_read_page->hdr.nd_read_offset ==
  63. ilctxt->write_page->hdr.write_offset));
  64. }
  65. /**
  66. * is_read_empty - Returns true if no data is available in log
  67. *
  68. * @ilctxt: logging context
  69. * @returns: > 1 if context is empty; 0 if not empty; <0 for failure
  70. *
  71. * This is for the actual log contents. If it is empty, then there
  72. * is no data at all in the log.
  73. */
  74. static int is_read_empty(struct ipc_log_context *ilctxt)
  75. {
  76. if (!ilctxt)
  77. return -EINVAL;
  78. return ((ilctxt->read_page == ilctxt->write_page) &&
  79. (ilctxt->read_page->hdr.read_offset ==
  80. ilctxt->write_page->hdr.write_offset));
  81. }
  82. /**
  83. * is_nd_read_equal_read - Return true if the non-destructive read is equal to
  84. * the destructive read
  85. *
  86. * @ilctxt: logging context
  87. * @returns: true if nd read is equal to read; false otherwise
  88. */
  89. static bool is_nd_read_equal_read(struct ipc_log_context *ilctxt)
  90. {
  91. uint16_t read_offset;
  92. uint16_t nd_read_offset;
  93. if (ilctxt->nd_read_page == ilctxt->read_page) {
  94. read_offset = ilctxt->read_page->hdr.read_offset;
  95. nd_read_offset = ilctxt->nd_read_page->hdr.nd_read_offset;
  96. if (read_offset == nd_read_offset)
  97. return true;
  98. }
  99. return false;
  100. }
  101. static struct ipc_log_page *get_next_page(struct ipc_log_context *ilctxt,
  102. struct ipc_log_page *cur_pg)
  103. {
  104. struct ipc_log_page_header *p_pghdr;
  105. struct ipc_log_page *pg = NULL;
  106. if (!ilctxt || !cur_pg)
  107. return NULL;
  108. if (ilctxt->last_page == cur_pg)
  109. return ilctxt->first_page;
  110. p_pghdr = list_first_entry(&cur_pg->hdr.list,
  111. struct ipc_log_page_header, list);
  112. pg = container_of(p_pghdr, struct ipc_log_page, hdr);
  113. return pg;
  114. }
  115. static void register_minidump(u64 vaddr, u64 size,
  116. const char *buf_name, int index)
  117. {
  118. struct md_region md_entry;
  119. int ret;
  120. if (msm_minidump_enabled()
  121. && (minidump_buf_cnt < MAX_MINIDUMP_BUFFERS)) {
  122. scnprintf(md_entry.name, sizeof(md_entry.name), "%s_%d",
  123. buf_name, index);
  124. md_entry.virt_addr = vaddr;
  125. md_entry.phys_addr = virt_to_phys((void *)vaddr);
  126. md_entry.size = size;
  127. ret = msm_minidump_add_region(&md_entry);
  128. if (ret < 0) {
  129. pr_err(
  130. "Failed to register log buffer %s_%d in Minidump ret %d\n",
  131. buf_name, index, ret);
  132. return;
  133. }
  134. minidump_buf_cnt++;
  135. }
  136. }
  137. /**
  138. * ipc_log_read - do non-destructive read of the log
  139. *
  140. * @ilctxt: Logging context
  141. * @data: Data pointer to receive the data
  142. * @data_size: Number of bytes to read (must be <= bytes available in log)
  143. *
  144. * This read will update a runtime read pointer, but will not affect the actual
  145. * contents of the log which allows for reading the logs continuously while
  146. * debugging and if the system crashes, then the full logs can still be
  147. * extracted.
  148. */
  149. static void ipc_log_read(struct ipc_log_context *ilctxt,
  150. void *data, int data_size)
  151. {
  152. int bytes_to_read;
  153. bytes_to_read = MIN(LOG_PAGE_DATA_SIZE
  154. - ilctxt->nd_read_page->hdr.nd_read_offset,
  155. data_size);
  156. memcpy(data, (ilctxt->nd_read_page->data +
  157. ilctxt->nd_read_page->hdr.nd_read_offset), bytes_to_read);
  158. if (bytes_to_read != data_size) {
  159. /* not enough space, wrap read to next page */
  160. ilctxt->nd_read_page->hdr.nd_read_offset = 0;
  161. ilctxt->nd_read_page = get_next_page(ilctxt,
  162. ilctxt->nd_read_page);
  163. if (WARN_ON(ilctxt->nd_read_page == NULL))
  164. return;
  165. memcpy((data + bytes_to_read),
  166. (ilctxt->nd_read_page->data +
  167. ilctxt->nd_read_page->hdr.nd_read_offset),
  168. (data_size - bytes_to_read));
  169. bytes_to_read = (data_size - bytes_to_read);
  170. }
  171. ilctxt->nd_read_page->hdr.nd_read_offset += bytes_to_read;
  172. }
  173. /**
  174. * ipc_log_drop - do destructive read of the log
  175. *
  176. * @ilctxt: Logging context
  177. * @data: Data pointer to receive the data (or NULL)
  178. * @data_size: Number of bytes to read (must be <= bytes available in log)
  179. */
  180. static void ipc_log_drop(struct ipc_log_context *ilctxt, void *data,
  181. int data_size)
  182. {
  183. int bytes_to_read;
  184. bool push_nd_read;
  185. bytes_to_read = MIN(LOG_PAGE_DATA_SIZE
  186. - ilctxt->read_page->hdr.read_offset,
  187. data_size);
  188. if (data)
  189. memcpy(data, (ilctxt->read_page->data +
  190. ilctxt->read_page->hdr.read_offset), bytes_to_read);
  191. if (bytes_to_read != data_size) {
  192. /* not enough space, wrap read to next page */
  193. push_nd_read = is_nd_read_equal_read(ilctxt);
  194. ilctxt->read_page->hdr.read_offset = 0;
  195. if (push_nd_read) {
  196. ilctxt->read_page->hdr.nd_read_offset = 0;
  197. ilctxt->read_page = get_next_page(ilctxt,
  198. ilctxt->read_page);
  199. if (WARN_ON(ilctxt->read_page == NULL))
  200. return;
  201. ilctxt->nd_read_page = ilctxt->read_page;
  202. } else {
  203. ilctxt->read_page = get_next_page(ilctxt,
  204. ilctxt->read_page);
  205. if (WARN_ON(ilctxt->read_page == NULL))
  206. return;
  207. }
  208. if (data)
  209. memcpy((data + bytes_to_read),
  210. (ilctxt->read_page->data +
  211. ilctxt->read_page->hdr.read_offset),
  212. (data_size - bytes_to_read));
  213. bytes_to_read = (data_size - bytes_to_read);
  214. }
  215. /* update non-destructive read pointer if necessary */
  216. push_nd_read = is_nd_read_equal_read(ilctxt);
  217. ilctxt->read_page->hdr.read_offset += bytes_to_read;
  218. ilctxt->write_avail += data_size;
  219. if (push_nd_read)
  220. ilctxt->nd_read_page->hdr.nd_read_offset += bytes_to_read;
  221. }
  222. /**
  223. * msg_read - Reads a message.
  224. *
  225. * If a message is read successfully, then the message context
  226. * will be set to:
  227. * .hdr message header .size and .type values
  228. * .offset beginning of message data
  229. *
  230. * @ilctxt Logging context
  231. * @ectxt Message context
  232. *
  233. * @returns 0 - no message available; >0 message size; <0 error
  234. */
  235. static int msg_read(struct ipc_log_context *ilctxt,
  236. struct encode_context *ectxt)
  237. {
  238. struct tsv_header hdr;
  239. if (!ectxt)
  240. return -EINVAL;
  241. if (is_nd_read_empty(ilctxt))
  242. return 0;
  243. ipc_log_read(ilctxt, &hdr, sizeof(hdr));
  244. ectxt->hdr.type = hdr.type;
  245. ectxt->hdr.size = hdr.size;
  246. ectxt->offset = sizeof(hdr);
  247. ipc_log_read(ilctxt, (ectxt->buff + ectxt->offset),
  248. (int)hdr.size);
  249. return sizeof(hdr) + (int)hdr.size;
  250. }
  251. /**
  252. * msg_drop - Drops a message.
  253. *
  254. * @ilctxt Logging context
  255. */
  256. static void msg_drop(struct ipc_log_context *ilctxt)
  257. {
  258. struct tsv_header hdr;
  259. if (!is_read_empty(ilctxt)) {
  260. ipc_log_drop(ilctxt, &hdr, sizeof(hdr));
  261. ipc_log_drop(ilctxt, NULL, (int)hdr.size);
  262. }
  263. }
  264. /*
  265. * Commits messages to the FIFO. If the FIFO is full, then enough
  266. * messages are dropped to create space for the new message.
  267. */
  268. static void __ipc_log_write(void *ctxt, struct encode_context *ectxt)
  269. {
  270. struct ipc_log_context *ilctxt = (struct ipc_log_context *)ctxt;
  271. int bytes_to_write;
  272. unsigned long flags;
  273. if (!ilctxt || !ectxt) {
  274. pr_err("%s: Invalid ipc_log or encode context\n", __func__);
  275. return;
  276. }
  277. read_lock_irqsave(&context_list_lock_lha1, flags);
  278. spin_lock(&ilctxt->context_lock_lhb1);
  279. while (ilctxt->write_avail <= ectxt->offset)
  280. msg_drop(ilctxt);
  281. bytes_to_write = MIN(LOG_PAGE_DATA_SIZE
  282. - ilctxt->write_page->hdr.write_offset,
  283. ectxt->offset);
  284. memcpy((ilctxt->write_page->data +
  285. ilctxt->write_page->hdr.write_offset),
  286. ectxt->buff, bytes_to_write);
  287. if (bytes_to_write != ectxt->offset) {
  288. uint64_t t_now = sched_clock();
  289. ilctxt->write_page->hdr.write_offset += bytes_to_write;
  290. ilctxt->write_page->hdr.end_time = t_now;
  291. ilctxt->write_page = get_next_page(ilctxt, ilctxt->write_page);
  292. if (WARN_ON(ilctxt->write_page == NULL)) {
  293. spin_unlock(&ilctxt->context_lock_lhb1);
  294. read_unlock_irqrestore(&context_list_lock_lha1, flags);
  295. return;
  296. }
  297. ilctxt->write_page->hdr.write_offset = 0;
  298. ilctxt->write_page->hdr.start_time = t_now;
  299. memcpy((ilctxt->write_page->data +
  300. ilctxt->write_page->hdr.write_offset),
  301. (ectxt->buff + bytes_to_write),
  302. (ectxt->offset - bytes_to_write));
  303. bytes_to_write = (ectxt->offset - bytes_to_write);
  304. }
  305. ilctxt->write_page->hdr.write_offset += bytes_to_write;
  306. ilctxt->write_avail -= ectxt->offset;
  307. complete(&ilctxt->read_avail);
  308. spin_unlock(&ilctxt->context_lock_lhb1);
  309. read_unlock_irqrestore(&context_list_lock_lha1, flags);
  310. }
  311. void ipc_log_write(void *ctxt, struct encode_context *ectxt)
  312. {
  313. if (!sec_debug_is_enabled())
  314. return;
  315. __ipc_log_write(ctxt, ectxt);
  316. }
  317. EXPORT_SYMBOL(ipc_log_write);
  318. /*
  319. * Starts a new message after which you can add serialized data and
  320. * then complete the message by calling msg_encode_end().
  321. */
  322. void msg_encode_start(struct encode_context *ectxt, uint32_t type)
  323. {
  324. if (!ectxt) {
  325. pr_err("%s: Invalid encode context\n", __func__);
  326. return;
  327. }
  328. ectxt->hdr.type = type;
  329. ectxt->hdr.size = 0;
  330. ectxt->offset = sizeof(ectxt->hdr);
  331. }
  332. EXPORT_SYMBOL(msg_encode_start);
  333. /*
  334. * Completes the message
  335. */
  336. void msg_encode_end(struct encode_context *ectxt)
  337. {
  338. if (!ectxt) {
  339. pr_err("%s: Invalid encode context\n", __func__);
  340. return;
  341. }
  342. /* finalize data size */
  343. ectxt->hdr.size = ectxt->offset - sizeof(ectxt->hdr);
  344. memcpy(ectxt->buff, &ectxt->hdr, sizeof(ectxt->hdr));
  345. }
  346. EXPORT_SYMBOL(msg_encode_end);
  347. /*
  348. * Helper function used to write data to a message context.
  349. *
  350. * @ectxt context initialized by calling msg_encode_start()
  351. * @data data to write
  352. * @size number of bytes of data to write
  353. */
  354. static inline int tsv_write_data(struct encode_context *ectxt,
  355. void *data, uint32_t size)
  356. {
  357. if (!ectxt) {
  358. pr_err("%s: Invalid encode context\n", __func__);
  359. return -EINVAL;
  360. }
  361. if ((ectxt->offset + size) > MAX_MSG_SIZE) {
  362. pr_err("%s: No space to encode further\n", __func__);
  363. return -EINVAL;
  364. }
  365. memcpy((void *)(ectxt->buff + ectxt->offset), data, size);
  366. ectxt->offset += size;
  367. return 0;
  368. }
  369. /*
  370. * Helper function that writes a type to the context.
  371. *
  372. * @ectxt context initialized by calling msg_encode_start()
  373. * @type primitive type
  374. * @size size of primitive in bytes
  375. */
  376. static inline int tsv_write_header(struct encode_context *ectxt,
  377. uint32_t type, uint32_t size)
  378. {
  379. struct tsv_header hdr;
  380. hdr.type = (unsigned char)type;
  381. hdr.size = (unsigned char)size;
  382. return tsv_write_data(ectxt, &hdr, sizeof(hdr));
  383. }
  384. /*
  385. * Writes the current timestamp count.
  386. *
  387. * @ectxt context initialized by calling msg_encode_start()
  388. */
  389. int tsv_timestamp_write(struct encode_context *ectxt)
  390. {
  391. int ret;
  392. uint64_t t_now = sched_clock();
  393. ret = tsv_write_header(ectxt, TSV_TYPE_TIMESTAMP, sizeof(t_now));
  394. if (ret)
  395. return ret;
  396. return tsv_write_data(ectxt, &t_now, sizeof(t_now));
  397. }
  398. EXPORT_SYMBOL(tsv_timestamp_write);
  399. /*
  400. * Writes the current QTimer timestamp count.
  401. *
  402. * @ectxt context initialized by calling msg_encode_start()
  403. */
  404. int tsv_qtimer_write(struct encode_context *ectxt)
  405. {
  406. int ret;
  407. uint64_t t_now = __arch_counter_get_cntvct();
  408. ret = tsv_write_header(ectxt, TSV_TYPE_QTIMER, sizeof(t_now));
  409. if (ret)
  410. return ret;
  411. return tsv_write_data(ectxt, &t_now, sizeof(t_now));
  412. }
  413. EXPORT_SYMBOL(tsv_qtimer_write);
  414. /*
  415. * Writes a data pointer.
  416. *
  417. * @ectxt context initialized by calling msg_encode_start()
  418. * @pointer pointer value to write
  419. */
  420. int tsv_pointer_write(struct encode_context *ectxt, void *pointer)
  421. {
  422. int ret;
  423. ret = tsv_write_header(ectxt, TSV_TYPE_POINTER, sizeof(pointer));
  424. if (ret)
  425. return ret;
  426. return tsv_write_data(ectxt, &pointer, sizeof(pointer));
  427. }
  428. EXPORT_SYMBOL(tsv_pointer_write);
  429. /*
  430. * Writes a 32-bit integer value.
  431. *
  432. * @ectxt context initialized by calling msg_encode_start()
  433. * @n integer to write
  434. */
  435. int tsv_int32_write(struct encode_context *ectxt, int32_t n)
  436. {
  437. int ret;
  438. ret = tsv_write_header(ectxt, TSV_TYPE_INT32, sizeof(n));
  439. if (ret)
  440. return ret;
  441. return tsv_write_data(ectxt, &n, sizeof(n));
  442. }
  443. EXPORT_SYMBOL(tsv_int32_write);
  444. /*
  445. * Writes a byte array.
  446. *
  447. * @ectxt context initialized by calling msg_write_start()
  448. * @data Beginning address of data
  449. * @data_size Size of data to be written
  450. */
  451. int tsv_byte_array_write(struct encode_context *ectxt,
  452. void *data, int data_size)
  453. {
  454. int ret;
  455. ret = tsv_write_header(ectxt, TSV_TYPE_BYTE_ARRAY, data_size);
  456. if (ret)
  457. return ret;
  458. return tsv_write_data(ectxt, data, data_size);
  459. }
  460. EXPORT_SYMBOL(tsv_byte_array_write);
  461. /*
  462. * Helper function to log a string
  463. *
  464. * @ilctxt ipc_log_context created using ipc_log_context_create()
  465. * @fmt Data specified using format specifiers
  466. */
  467. int ipc_log_string(void *ilctxt, const char *fmt, ...)
  468. {
  469. struct encode_context ectxt;
  470. int avail_size, data_size, hdr_size = sizeof(struct tsv_header);
  471. va_list arg_list;
  472. if (!sec_debug_is_enabled())
  473. return 0;
  474. if (!ilctxt)
  475. return -EINVAL;
  476. msg_encode_start(&ectxt, TSV_TYPE_STRING);
  477. tsv_timestamp_write(&ectxt);
  478. tsv_qtimer_write(&ectxt);
  479. avail_size = (MAX_MSG_SIZE - (ectxt.offset + hdr_size));
  480. va_start(arg_list, fmt);
  481. data_size = vscnprintf((ectxt.buff + ectxt.offset + hdr_size),
  482. avail_size, fmt, arg_list);
  483. va_end(arg_list);
  484. tsv_write_header(&ectxt, TSV_TYPE_BYTE_ARRAY, data_size);
  485. ectxt.offset += data_size;
  486. msg_encode_end(&ectxt);
  487. ipc_log_write(ilctxt, &ectxt);
  488. return 0;
  489. }
  490. EXPORT_SYMBOL(ipc_log_string);
  491. /**
  492. * ipc_log_extract - Reads and deserializes log
  493. *
  494. * @ctxt: logging context
  495. * @buff: buffer to receive the data
  496. * @size: size of the buffer
  497. * @returns: 0 if no data read; >0 number of bytes read; < 0 error
  498. *
  499. * If no data is available to be read, then the ilctxt::read_avail
  500. * completion is reinitialized. This allows clients to block
  501. * until new log data is save.
  502. */
  503. int ipc_log_extract(void *ctxt, char *buff, int size)
  504. {
  505. struct encode_context ectxt;
  506. struct decode_context dctxt;
  507. void (*deserialize_func)(struct encode_context *ectxt,
  508. struct decode_context *dctxt);
  509. struct ipc_log_context *ilctxt = (struct ipc_log_context *)ctxt;
  510. unsigned long flags;
  511. int ret;
  512. if (size < MAX_MSG_DECODED_SIZE)
  513. return -EINVAL;
  514. dctxt.output_format = OUTPUT_DEBUGFS;
  515. dctxt.buff = buff;
  516. dctxt.size = size;
  517. read_lock_irqsave(&context_list_lock_lha1, flags);
  518. spin_lock(&ilctxt->context_lock_lhb1);
  519. if (ilctxt->destroyed) {
  520. ret = -EIO;
  521. goto done;
  522. }
  523. while (dctxt.size >= MAX_MSG_DECODED_SIZE &&
  524. !is_nd_read_empty(ilctxt)) {
  525. msg_read(ilctxt, &ectxt);
  526. deserialize_func = get_deserialization_func(ilctxt,
  527. ectxt.hdr.type);
  528. spin_unlock(&ilctxt->context_lock_lhb1);
  529. read_unlock_irqrestore(&context_list_lock_lha1, flags);
  530. if (deserialize_func)
  531. deserialize_func(&ectxt, &dctxt);
  532. else
  533. pr_err("%s: unknown message 0x%x\n",
  534. __func__, ectxt.hdr.type);
  535. read_lock_irqsave(&context_list_lock_lha1, flags);
  536. spin_lock(&ilctxt->context_lock_lhb1);
  537. }
  538. ret = size - dctxt.size;
  539. if (ret == 0) {
  540. if (!ilctxt->destroyed)
  541. reinit_completion(&ilctxt->read_avail);
  542. else
  543. ret = -EIO;
  544. }
  545. done:
  546. spin_unlock(&ilctxt->context_lock_lhb1);
  547. read_unlock_irqrestore(&context_list_lock_lha1, flags);
  548. return ret;
  549. }
  550. EXPORT_SYMBOL(ipc_log_extract);
  551. /*
  552. * Helper function used to read data from a message context.
  553. *
  554. * @ectxt context initialized by calling msg_read()
  555. * @data data to read
  556. * @size number of bytes of data to read
  557. */
  558. static void tsv_read_data(struct encode_context *ectxt,
  559. void *data, uint32_t size)
  560. {
  561. if (WARN_ON((ectxt->offset + size) > MAX_MSG_SIZE)) {
  562. memcpy(data, (ectxt->buff + ectxt->offset),
  563. MAX_MSG_SIZE - ectxt->offset - 1);
  564. ectxt->offset += MAX_MSG_SIZE - ectxt->offset - 1;
  565. return;
  566. }
  567. memcpy(data, (ectxt->buff + ectxt->offset), size);
  568. ectxt->offset += size;
  569. }
  570. /*
  571. * Helper function that reads a type from the context and updates the
  572. * context pointers.
  573. *
  574. * @ectxt context initialized by calling msg_read()
  575. * @hdr type header
  576. */
  577. static void tsv_read_header(struct encode_context *ectxt,
  578. struct tsv_header *hdr)
  579. {
  580. if (WARN_ON((ectxt->offset + sizeof(*hdr)) > MAX_MSG_SIZE)) {
  581. memcpy(hdr, (ectxt->buff + ectxt->offset),
  582. MAX_MSG_SIZE - ectxt->offset - 1);
  583. ectxt->offset += MAX_MSG_SIZE - ectxt->offset - 1;
  584. return;
  585. }
  586. memcpy(hdr, (ectxt->buff + ectxt->offset), sizeof(*hdr));
  587. ectxt->offset += sizeof(*hdr);
  588. }
  589. /*
  590. * Reads a timestamp.
  591. *
  592. * @ectxt context initialized by calling msg_read()
  593. * @dctxt deserialization context
  594. * @format output format (appended to %6u.09u timestamp format)
  595. */
  596. void tsv_timestamp_read(struct encode_context *ectxt,
  597. struct decode_context *dctxt, const char *format)
  598. {
  599. struct tsv_header hdr;
  600. uint64_t val;
  601. unsigned long nanosec_rem;
  602. tsv_read_header(ectxt, &hdr);
  603. if (WARN_ON(hdr.type != TSV_TYPE_TIMESTAMP))
  604. return;
  605. tsv_read_data(ectxt, &val, sizeof(val));
  606. nanosec_rem = do_div(val, 1000000000U);
  607. IPC_SPRINTF_DECODE(dctxt, "[%6u.%09lu%s/",
  608. (unsigned int)val, nanosec_rem, format);
  609. }
  610. EXPORT_SYMBOL(tsv_timestamp_read);
  611. /*
  612. * Reads a QTimer timestamp.
  613. *
  614. * @ectxt context initialized by calling msg_read()
  615. * @dctxt deserialization context
  616. * @format output format (appended to %#18llx timestamp format)
  617. */
  618. void tsv_qtimer_read(struct encode_context *ectxt,
  619. struct decode_context *dctxt, const char *format)
  620. {
  621. struct tsv_header hdr;
  622. uint64_t val;
  623. tsv_read_header(ectxt, &hdr);
  624. if (WARN_ON(hdr.type != TSV_TYPE_QTIMER))
  625. return;
  626. tsv_read_data(ectxt, &val, sizeof(val));
  627. /*
  628. * This gives 16 hex digits of output. The # prefix prepends
  629. * a 0x, and these characters count as part of the number.
  630. */
  631. IPC_SPRINTF_DECODE(dctxt, "%#18llx]%s", val, format);
  632. }
  633. EXPORT_SYMBOL(tsv_qtimer_read);
  634. /*
  635. * Reads a data pointer.
  636. *
  637. * @ectxt context initialized by calling msg_read()
  638. * @dctxt deserialization context
  639. * @format output format
  640. */
  641. void tsv_pointer_read(struct encode_context *ectxt,
  642. struct decode_context *dctxt, const char *format)
  643. {
  644. struct tsv_header hdr;
  645. void *val;
  646. tsv_read_header(ectxt, &hdr);
  647. if (WARN_ON(hdr.type != TSV_TYPE_POINTER))
  648. return;
  649. tsv_read_data(ectxt, &val, sizeof(val));
  650. IPC_SPRINTF_DECODE(dctxt, format, val);
  651. }
  652. EXPORT_SYMBOL(tsv_pointer_read);
  653. /*
  654. * Reads a 32-bit integer value.
  655. *
  656. * @ectxt context initialized by calling msg_read()
  657. * @dctxt deserialization context
  658. * @format output format
  659. */
  660. int32_t tsv_int32_read(struct encode_context *ectxt,
  661. struct decode_context *dctxt, const char *format)
  662. {
  663. struct tsv_header hdr;
  664. int32_t val;
  665. tsv_read_header(ectxt, &hdr);
  666. if (WARN_ON(hdr.type != TSV_TYPE_INT32))
  667. return -EINVAL;
  668. tsv_read_data(ectxt, &val, sizeof(val));
  669. IPC_SPRINTF_DECODE(dctxt, format, val);
  670. return val;
  671. }
  672. EXPORT_SYMBOL(tsv_int32_read);
  673. /*
  674. * Reads a byte array/string.
  675. *
  676. * @ectxt context initialized by calling msg_read()
  677. * @dctxt deserialization context
  678. * @format output format
  679. */
  680. void tsv_byte_array_read(struct encode_context *ectxt,
  681. struct decode_context *dctxt, const char *format)
  682. {
  683. struct tsv_header hdr;
  684. tsv_read_header(ectxt, &hdr);
  685. if (WARN_ON(hdr.type != TSV_TYPE_BYTE_ARRAY))
  686. return;
  687. tsv_read_data(ectxt, dctxt->buff, hdr.size);
  688. dctxt->buff += hdr.size;
  689. dctxt->size -= hdr.size;
  690. }
  691. EXPORT_SYMBOL(tsv_byte_array_read);
  692. int add_deserialization_func(void *ctxt, int type,
  693. void (*dfunc)(struct encode_context *,
  694. struct decode_context *))
  695. {
  696. struct ipc_log_context *ilctxt = (struct ipc_log_context *)ctxt;
  697. struct dfunc_info *df_info;
  698. unsigned long flags;
  699. if (!ilctxt || !dfunc)
  700. return -EINVAL;
  701. df_info = kmalloc(sizeof(struct dfunc_info), GFP_KERNEL);
  702. if (!df_info)
  703. return -ENOSPC;
  704. read_lock_irqsave(&context_list_lock_lha1, flags);
  705. spin_lock(&ilctxt->context_lock_lhb1);
  706. df_info->type = type;
  707. df_info->dfunc = dfunc;
  708. list_add_tail(&df_info->list, &ilctxt->dfunc_info_list);
  709. spin_unlock(&ilctxt->context_lock_lhb1);
  710. read_unlock_irqrestore(&context_list_lock_lha1, flags);
  711. return 0;
  712. }
  713. EXPORT_SYMBOL(add_deserialization_func);
  714. static void *get_deserialization_func(struct ipc_log_context *ilctxt,
  715. int type)
  716. {
  717. struct dfunc_info *df_info = NULL;
  718. if (!ilctxt)
  719. return NULL;
  720. list_for_each_entry(df_info, &ilctxt->dfunc_info_list, list) {
  721. if (df_info->type == type)
  722. return df_info->dfunc;
  723. }
  724. return NULL;
  725. }
  726. /**
  727. * ipc_log_context_create: Create a debug log context if context does not exist.
  728. * Should not be called from atomic context
  729. *
  730. * @max_num_pages: Number of pages of logging space required (max. 10)
  731. * @mod_name : Name of the directory entry under DEBUGFS
  732. * @feature_version : First 16 bit for version number of user-defined message
  733. * formats and next 16 bit for enabling minidump
  734. *
  735. * returns context id on success, NULL on failure
  736. */
  737. static void *__ipc_log_context_create(int max_num_pages,
  738. const char *mod_name, uint32_t feature_version)
  739. {
  740. struct ipc_log_context *ctxt = NULL, *tmp;
  741. struct ipc_log_page *pg = NULL;
  742. int page_cnt;
  743. unsigned long flags;
  744. int enable_minidump;
  745. /* check if ipc ctxt already exists */
  746. read_lock_irq(&context_list_lock_lha1);
  747. list_for_each_entry(tmp, &ipc_log_context_list, list)
  748. if (!strcmp(tmp->name, mod_name)) {
  749. ctxt = tmp;
  750. break;
  751. }
  752. read_unlock_irq(&context_list_lock_lha1);
  753. if (ctxt)
  754. return ctxt;
  755. ctxt = kzalloc(sizeof(struct ipc_log_context), GFP_KERNEL);
  756. if (!ctxt)
  757. return 0;
  758. init_completion(&ctxt->read_avail);
  759. INIT_LIST_HEAD(&ctxt->page_list);
  760. INIT_LIST_HEAD(&ctxt->dfunc_info_list);
  761. spin_lock_init(&ctxt->context_lock_lhb1);
  762. enable_minidump = feature_version & FEATURE_MASK;
  763. spin_lock_irqsave(&ctxt->context_lock_lhb1, flags);
  764. if (enable_minidump) {
  765. register_minidump((u64)ctxt, sizeof(struct ipc_log_context),
  766. "ipc_ctxt", minidump_buf_cnt);
  767. }
  768. spin_unlock_irqrestore(&ctxt->context_lock_lhb1, flags);
  769. for (page_cnt = 0; page_cnt < max_num_pages; page_cnt++) {
  770. pg = kzalloc(sizeof(struct ipc_log_page), GFP_KERNEL);
  771. if (!pg)
  772. goto release_ipc_log_context;
  773. pg->hdr.log_id = (uint64_t)(uintptr_t)ctxt;
  774. pg->hdr.page_num = LOG_PAGE_FLAG | page_cnt;
  775. pg->hdr.ctx_offset = (int64_t)((uint64_t)(uintptr_t)ctxt -
  776. (uint64_t)(uintptr_t)&pg->hdr);
  777. /* set magic last to signal that page init is complete */
  778. pg->hdr.magic = IPC_LOGGING_MAGIC_NUM;
  779. pg->hdr.nmagic = ~(IPC_LOGGING_MAGIC_NUM);
  780. spin_lock_irqsave(&ctxt->context_lock_lhb1, flags);
  781. list_add_tail(&pg->hdr.list, &ctxt->page_list);
  782. if (enable_minidump) {
  783. register_minidump((u64)pg, sizeof(struct ipc_log_page),
  784. mod_name, minidump_buf_cnt);
  785. }
  786. spin_unlock_irqrestore(&ctxt->context_lock_lhb1, flags);
  787. }
  788. ctxt->log_id = (uint64_t)(uintptr_t)ctxt;
  789. ctxt->version = IPC_LOG_VERSION;
  790. strscpy(ctxt->name, mod_name, IPC_LOG_MAX_CONTEXT_NAME_LEN);
  791. ctxt->user_version = feature_version & 0xffff;
  792. ctxt->first_page = get_first_page(ctxt);
  793. ctxt->last_page = pg;
  794. ctxt->write_page = ctxt->first_page;
  795. ctxt->read_page = ctxt->first_page;
  796. ctxt->nd_read_page = ctxt->first_page;
  797. ctxt->write_avail = max_num_pages * LOG_PAGE_DATA_SIZE;
  798. ctxt->header_size = sizeof(struct ipc_log_page_header);
  799. kref_init(&ctxt->refcount);
  800. ctxt->destroyed = false;
  801. create_ctx_debugfs(ctxt, mod_name);
  802. ipc_log_cdev_create(ctxt, mod_name);
  803. /* set magic last to signal context init is complete */
  804. ctxt->magic = IPC_LOG_CONTEXT_MAGIC_NUM;
  805. ctxt->nmagic = ~(IPC_LOG_CONTEXT_MAGIC_NUM);
  806. write_lock_irqsave(&context_list_lock_lha1, flags);
  807. if (enable_minidump && (minidump_buf_cnt < MAX_MINIDUMP_BUFFERS))
  808. list_add(&ctxt->list, &ipc_log_context_list);
  809. else
  810. list_add_tail(&ctxt->list, &ipc_log_context_list);
  811. write_unlock_irqrestore(&context_list_lock_lha1, flags);
  812. return (void *)ctxt;
  813. release_ipc_log_context:
  814. while (page_cnt-- > 0) {
  815. pg = get_first_page(ctxt);
  816. list_del(&pg->hdr.list);
  817. kfree(pg);
  818. }
  819. kfree(ctxt);
  820. return 0;
  821. }
  822. void *ipc_log_context_create(int max_num_pages,
  823. const char *mod_name, uint32_t feature_version)
  824. {
  825. if (sec_debug_is_enabled() || !debug_level_dummy_ctx)
  826. return __ipc_log_context_create(max_num_pages, mod_name, feature_version);
  827. return debug_level_dummy_ctx;
  828. }
  829. EXPORT_SYMBOL(ipc_log_context_create);
  830. void ipc_log_context_free(struct kref *kref)
  831. {
  832. struct ipc_log_context *ilctxt = container_of(kref,
  833. struct ipc_log_context, refcount);
  834. struct ipc_log_page *pg = NULL;
  835. while (!list_empty(&ilctxt->page_list)) {
  836. pg = get_first_page(ilctxt);
  837. list_del(&pg->hdr.list);
  838. kfree(pg);
  839. }
  840. kfree(ilctxt);
  841. }
  842. EXPORT_SYMBOL(ipc_log_context_free);
  843. /*
  844. * Destroy debug log context
  845. *
  846. * @ctxt: debug log context created by calling ipc_log_context_create API.
  847. */
  848. static int __ipc_log_context_destroy(void *ctxt)
  849. {
  850. struct ipc_log_context *ilctxt = (struct ipc_log_context *)ctxt;
  851. struct dfunc_info *df_info = NULL, *tmp = NULL;
  852. unsigned long flags;
  853. if (!ilctxt)
  854. return 0;
  855. debugfs_remove_recursive(ilctxt->dent);
  856. ipc_log_cdev_remove(ilctxt);
  857. spin_lock(&ilctxt->context_lock_lhb1);
  858. ilctxt->destroyed = true;
  859. complete_all(&ilctxt->read_avail);
  860. list_for_each_entry_safe(df_info, tmp, &ilctxt->dfunc_info_list, list) {
  861. list_del(&df_info->list);
  862. kfree(df_info);
  863. }
  864. spin_unlock(&ilctxt->context_lock_lhb1);
  865. write_lock_irqsave(&context_list_lock_lha1, flags);
  866. list_del(&ilctxt->list);
  867. write_unlock_irqrestore(&context_list_lock_lha1, flags);
  868. ipc_log_context_put(ilctxt);
  869. return 0;
  870. }
  871. int ipc_log_context_destroy(void *ctxt)
  872. {
  873. if (sec_debug_is_enabled() || (ctxt != (void *)debug_level_dummy_ctx))
  874. return __ipc_log_context_destroy(ctxt);
  875. return 0;
  876. }
  877. EXPORT_SYMBOL(ipc_log_context_destroy);
  878. #define LOG_CTX_PAGE_CNT 150
  879. static void *log_ctx;
  880. #define MAX_LINE_SIZE 512
  881. static char *buf;
  882. void net_log(const char *fmt, ...)
  883. {
  884. va_list arg_list;
  885. va_start(arg_list, fmt);
  886. if (log_ctx && buf) {
  887. vscnprintf(buf, MAX_LINE_SIZE, fmt, arg_list);
  888. ipc_log_string(log_ctx, "%s", buf);
  889. }
  890. va_end(arg_list);
  891. }
  892. EXPORT_SYMBOL_GPL(net_log);
  893. static int __init net_ipc_log_init(void)
  894. {
  895. if (!log_ctx)
  896. log_ctx = ipc_log_context_create(LOG_CTX_PAGE_CNT,
  897. "net_log", 0);
  898. if (!buf)
  899. buf = kmalloc(MAX_LINE_SIZE, GFP_KERNEL);
  900. // error ????
  901. //
  902. return 0;
  903. }
  904. static void __exit net_ipc_log_exit(void)
  905. {
  906. if (log_ctx)
  907. ipc_log_context_destroy(log_ctx);
  908. if (buf)
  909. kfree(buf);
  910. }
  911. static int __init __create_dummy_ipc_context(void)
  912. {
  913. if (sec_debug_is_enabled())
  914. return 0;
  915. if (!debug_level_dummy_ctx)
  916. debug_level_dummy_ctx = __ipc_log_context_create(1, "dummy_log", 0);
  917. return 0;
  918. }
  919. static void __exit __destory_dummy_ipc_context(void)
  920. {
  921. if (sec_debug_is_enabled())
  922. return;
  923. if (debug_level_dummy_ctx)
  924. __ipc_log_context_destroy(debug_level_dummy_ctx);
  925. }
  926. static int __init ipc_logging_init(void)
  927. {
  928. check_and_create_debugfs();
  929. ipc_log_cdev_init();
  930. register_minidump((u64)&ipc_log_context_list, sizeof(struct list_head),
  931. "ipc_log_ctxt_list", minidump_buf_cnt);
  932. __create_dummy_ipc_context();
  933. net_ipc_log_init();
  934. return 0;
  935. }
  936. static void __exit ipc_logging_exit(void)
  937. {
  938. net_ipc_log_exit();
  939. __destory_dummy_ipc_context();
  940. }
  941. module_init(ipc_logging_init);
  942. module_exit(ipc_logging_exit);
  943. MODULE_DESCRIPTION("ipc logging");
  944. MODULE_LICENSE("GPL");