kprobes.c 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624
  1. /*
  2. * Kernel Probes (KProbes)
  3. * kernel/kprobes.c
  4. *
  5. * This program is free software; you can redistribute it and/or modify
  6. * it under the terms of the GNU General Public License as published by
  7. * the Free Software Foundation; either version 2 of the License, or
  8. * (at your option) any later version.
  9. *
  10. * This program is distributed in the hope that it will be useful,
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. * GNU General Public License for more details.
  14. *
  15. * You should have received a copy of the GNU General Public License
  16. * along with this program; if not, write to the Free Software
  17. * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
  18. *
  19. * Copyright (C) IBM Corporation, 2002, 2004
  20. *
  21. * 2002-Oct Created by Vamsi Krishna S <vamsi_krishna@in.ibm.com> Kernel
  22. * Probes initial implementation (includes suggestions from
  23. * Rusty Russell).
  24. * 2004-Aug Updated by Prasanna S Panchamukhi <prasanna@in.ibm.com> with
  25. * hlists and exceptions notifier as suggested by Andi Kleen.
  26. * 2004-July Suparna Bhattacharya <suparna@in.ibm.com> added jumper probes
  27. * interface to access function arguments.
  28. * 2004-Sep Prasanna S Panchamukhi <prasanna@in.ibm.com> Changed Kprobes
  29. * exceptions notifier to be first on the priority list.
  30. * 2005-May Hien Nguyen <hien@us.ibm.com>, Jim Keniston
  31. * <jkenisto@us.ibm.com> and Prasanna S Panchamukhi
  32. * <prasanna@in.ibm.com> added function-return probes.
  33. */
  34. #include <linux/kprobes.h>
  35. #include <linux/spinlock.h>
  36. #include <linux/hash.h>
  37. #include <linux/init.h>
  38. #include <linux/module.h>
  39. #include <linux/moduleloader.h>
  40. #include <asm/cacheflush.h>
  41. #include <asm/errno.h>
  42. #include <asm/kdebug.h>
  43. #define KPROBE_HASH_BITS 6
  44. #define KPROBE_TABLE_SIZE (1 << KPROBE_HASH_BITS)
  45. static struct hlist_head kprobe_table[KPROBE_TABLE_SIZE];
  46. static struct hlist_head kretprobe_inst_table[KPROBE_TABLE_SIZE];
  47. unsigned int kprobe_cpu = NR_CPUS;
  48. static DEFINE_SPINLOCK(kprobe_lock);
  49. static struct kprobe *curr_kprobe;
  50. /*
  51. * kprobe->ainsn.insn points to the copy of the instruction to be
  52. * single-stepped. x86_64, POWER4 and above have no-exec support and
  53. * stepping on the instruction on a vmalloced/kmalloced/data page
  54. * is a recipe for disaster
  55. */
  56. #define INSNS_PER_PAGE (PAGE_SIZE/(MAX_INSN_SIZE * sizeof(kprobe_opcode_t)))
  57. struct kprobe_insn_page {
  58. struct hlist_node hlist;
  59. kprobe_opcode_t *insns; /* Page of instruction slots */
  60. char slot_used[INSNS_PER_PAGE];
  61. int nused;
  62. };
  63. static struct hlist_head kprobe_insn_pages;
  64. /**
  65. * get_insn_slot() - Find a slot on an executable page for an instruction.
  66. * We allocate an executable page if there's no room on existing ones.
  67. */
  68. kprobe_opcode_t *get_insn_slot(void)
  69. {
  70. struct kprobe_insn_page *kip;
  71. struct hlist_node *pos;
  72. hlist_for_each(pos, &kprobe_insn_pages) {
  73. kip = hlist_entry(pos, struct kprobe_insn_page, hlist);
  74. if (kip->nused < INSNS_PER_PAGE) {
  75. int i;
  76. for (i = 0; i < INSNS_PER_PAGE; i++) {
  77. if (!kip->slot_used[i]) {
  78. kip->slot_used[i] = 1;
  79. kip->nused++;
  80. return kip->insns + (i * MAX_INSN_SIZE);
  81. }
  82. }
  83. /* Surprise! No unused slots. Fix kip->nused. */
  84. kip->nused = INSNS_PER_PAGE;
  85. }
  86. }
  87. /* All out of space. Need to allocate a new page. Use slot 0.*/
  88. kip = kmalloc(sizeof(struct kprobe_insn_page), GFP_KERNEL);
  89. if (!kip) {
  90. return NULL;
  91. }
  92. /*
  93. * Use module_alloc so this page is within +/- 2GB of where the
  94. * kernel image and loaded module images reside. This is required
  95. * so x86_64 can correctly handle the %rip-relative fixups.
  96. */
  97. kip->insns = module_alloc(PAGE_SIZE);
  98. if (!kip->insns) {
  99. kfree(kip);
  100. return NULL;
  101. }
  102. INIT_HLIST_NODE(&kip->hlist);
  103. hlist_add_head(&kip->hlist, &kprobe_insn_pages);
  104. memset(kip->slot_used, 0, INSNS_PER_PAGE);
  105. kip->slot_used[0] = 1;
  106. kip->nused = 1;
  107. return kip->insns;
  108. }
  109. void free_insn_slot(kprobe_opcode_t *slot)
  110. {
  111. struct kprobe_insn_page *kip;
  112. struct hlist_node *pos;
  113. hlist_for_each(pos, &kprobe_insn_pages) {
  114. kip = hlist_entry(pos, struct kprobe_insn_page, hlist);
  115. if (kip->insns <= slot &&
  116. slot < kip->insns + (INSNS_PER_PAGE * MAX_INSN_SIZE)) {
  117. int i = (slot - kip->insns) / MAX_INSN_SIZE;
  118. kip->slot_used[i] = 0;
  119. kip->nused--;
  120. if (kip->nused == 0) {
  121. /*
  122. * Page is no longer in use. Free it unless
  123. * it's the last one. We keep the last one
  124. * so as not to have to set it up again the
  125. * next time somebody inserts a probe.
  126. */
  127. hlist_del(&kip->hlist);
  128. if (hlist_empty(&kprobe_insn_pages)) {
  129. INIT_HLIST_NODE(&kip->hlist);
  130. hlist_add_head(&kip->hlist,
  131. &kprobe_insn_pages);
  132. } else {
  133. module_free(NULL, kip->insns);
  134. kfree(kip);
  135. }
  136. }
  137. return;
  138. }
  139. }
  140. }
  141. /* Locks kprobe: irqs must be disabled */
  142. void lock_kprobes(void)
  143. {
  144. spin_lock(&kprobe_lock);
  145. kprobe_cpu = smp_processor_id();
  146. }
  147. void unlock_kprobes(void)
  148. {
  149. kprobe_cpu = NR_CPUS;
  150. spin_unlock(&kprobe_lock);
  151. }
  152. /* You have to be holding the kprobe_lock */
  153. struct kprobe *get_kprobe(void *addr)
  154. {
  155. struct hlist_head *head;
  156. struct hlist_node *node;
  157. head = &kprobe_table[hash_ptr(addr, KPROBE_HASH_BITS)];
  158. hlist_for_each(node, head) {
  159. struct kprobe *p = hlist_entry(node, struct kprobe, hlist);
  160. if (p->addr == addr)
  161. return p;
  162. }
  163. return NULL;
  164. }
  165. /*
  166. * Aggregate handlers for multiple kprobes support - these handlers
  167. * take care of invoking the individual kprobe handlers on p->list
  168. */
  169. static int aggr_pre_handler(struct kprobe *p, struct pt_regs *regs)
  170. {
  171. struct kprobe *kp;
  172. list_for_each_entry(kp, &p->list, list) {
  173. if (kp->pre_handler) {
  174. curr_kprobe = kp;
  175. if (kp->pre_handler(kp, regs))
  176. return 1;
  177. }
  178. curr_kprobe = NULL;
  179. }
  180. return 0;
  181. }
  182. static void aggr_post_handler(struct kprobe *p, struct pt_regs *regs,
  183. unsigned long flags)
  184. {
  185. struct kprobe *kp;
  186. list_for_each_entry(kp, &p->list, list) {
  187. if (kp->post_handler) {
  188. curr_kprobe = kp;
  189. kp->post_handler(kp, regs, flags);
  190. curr_kprobe = NULL;
  191. }
  192. }
  193. return;
  194. }
  195. static int aggr_fault_handler(struct kprobe *p, struct pt_regs *regs,
  196. int trapnr)
  197. {
  198. /*
  199. * if we faulted "during" the execution of a user specified
  200. * probe handler, invoke just that probe's fault handler
  201. */
  202. if (curr_kprobe && curr_kprobe->fault_handler) {
  203. if (curr_kprobe->fault_handler(curr_kprobe, regs, trapnr))
  204. return 1;
  205. }
  206. return 0;
  207. }
  208. static int aggr_break_handler(struct kprobe *p, struct pt_regs *regs)
  209. {
  210. struct kprobe *kp = curr_kprobe;
  211. if (curr_kprobe && kp->break_handler) {
  212. if (kp->break_handler(kp, regs)) {
  213. curr_kprobe = NULL;
  214. return 1;
  215. }
  216. }
  217. curr_kprobe = NULL;
  218. return 0;
  219. }
  220. struct kprobe trampoline_p = {
  221. .addr = (kprobe_opcode_t *) &kretprobe_trampoline,
  222. .pre_handler = trampoline_probe_handler,
  223. .post_handler = trampoline_post_handler
  224. };
  225. struct kretprobe_instance *get_free_rp_inst(struct kretprobe *rp)
  226. {
  227. struct hlist_node *node;
  228. struct kretprobe_instance *ri;
  229. hlist_for_each_entry(ri, node, &rp->free_instances, uflist)
  230. return ri;
  231. return NULL;
  232. }
  233. static struct kretprobe_instance *get_used_rp_inst(struct kretprobe *rp)
  234. {
  235. struct hlist_node *node;
  236. struct kretprobe_instance *ri;
  237. hlist_for_each_entry(ri, node, &rp->used_instances, uflist)
  238. return ri;
  239. return NULL;
  240. }
  241. struct kretprobe_instance *get_rp_inst(void *sara)
  242. {
  243. struct hlist_head *head;
  244. struct hlist_node *node;
  245. struct task_struct *tsk;
  246. struct kretprobe_instance *ri;
  247. tsk = arch_get_kprobe_task(sara);
  248. head = &kretprobe_inst_table[hash_ptr(tsk, KPROBE_HASH_BITS)];
  249. hlist_for_each_entry(ri, node, head, hlist) {
  250. if (ri->stack_addr == sara)
  251. return ri;
  252. }
  253. return NULL;
  254. }
  255. void add_rp_inst(struct kretprobe_instance *ri)
  256. {
  257. struct task_struct *tsk;
  258. /*
  259. * Remove rp inst off the free list -
  260. * Add it back when probed function returns
  261. */
  262. hlist_del(&ri->uflist);
  263. tsk = arch_get_kprobe_task(ri->stack_addr);
  264. /* Add rp inst onto table */
  265. INIT_HLIST_NODE(&ri->hlist);
  266. hlist_add_head(&ri->hlist,
  267. &kretprobe_inst_table[hash_ptr(tsk, KPROBE_HASH_BITS)]);
  268. /* Also add this rp inst to the used list. */
  269. INIT_HLIST_NODE(&ri->uflist);
  270. hlist_add_head(&ri->uflist, &ri->rp->used_instances);
  271. }
  272. void recycle_rp_inst(struct kretprobe_instance *ri)
  273. {
  274. /* remove rp inst off the rprobe_inst_table */
  275. hlist_del(&ri->hlist);
  276. if (ri->rp) {
  277. /* remove rp inst off the used list */
  278. hlist_del(&ri->uflist);
  279. /* put rp inst back onto the free list */
  280. INIT_HLIST_NODE(&ri->uflist);
  281. hlist_add_head(&ri->uflist, &ri->rp->free_instances);
  282. } else
  283. /* Unregistering */
  284. kfree(ri);
  285. }
  286. struct hlist_head * kretprobe_inst_table_head(struct task_struct *tsk)
  287. {
  288. return &kretprobe_inst_table[hash_ptr(tsk, KPROBE_HASH_BITS)];
  289. }
  290. struct kretprobe_instance *get_rp_inst_tsk(struct task_struct *tk)
  291. {
  292. struct task_struct *tsk;
  293. struct hlist_head *head;
  294. struct hlist_node *node;
  295. struct kretprobe_instance *ri;
  296. head = &kretprobe_inst_table[hash_ptr(tk, KPROBE_HASH_BITS)];
  297. hlist_for_each_entry(ri, node, head, hlist) {
  298. tsk = arch_get_kprobe_task(ri->stack_addr);
  299. if (tsk == tk)
  300. return ri;
  301. }
  302. return NULL;
  303. }
  304. /*
  305. * This function is called from do_exit or do_execv when task tk's stack is
  306. * about to be recycled. Recycle any function-return probe instances
  307. * associated with this task. These represent probed functions that have
  308. * been called but may never return.
  309. */
  310. void kprobe_flush_task(struct task_struct *tk)
  311. {
  312. unsigned long flags = 0;
  313. spin_lock_irqsave(&kprobe_lock, flags);
  314. arch_kprobe_flush_task(tk);
  315. spin_unlock_irqrestore(&kprobe_lock, flags);
  316. }
  317. /*
  318. * This kprobe pre_handler is registered with every kretprobe. When probe
  319. * hits it will set up the return probe.
  320. */
  321. static int pre_handler_kretprobe(struct kprobe *p, struct pt_regs *regs)
  322. {
  323. struct kretprobe *rp = container_of(p, struct kretprobe, kp);
  324. /*TODO: consider to only swap the RA after the last pre_handler fired */
  325. arch_prepare_kretprobe(rp, regs);
  326. return 0;
  327. }
  328. static inline void free_rp_inst(struct kretprobe *rp)
  329. {
  330. struct kretprobe_instance *ri;
  331. while ((ri = get_free_rp_inst(rp)) != NULL) {
  332. hlist_del(&ri->uflist);
  333. kfree(ri);
  334. }
  335. }
  336. /*
  337. * Keep all fields in the kprobe consistent
  338. */
  339. static inline void copy_kprobe(struct kprobe *old_p, struct kprobe *p)
  340. {
  341. memcpy(&p->opcode, &old_p->opcode, sizeof(kprobe_opcode_t));
  342. memcpy(&p->ainsn, &old_p->ainsn, sizeof(struct arch_specific_insn));
  343. }
  344. /*
  345. * Add the new probe to old_p->list. Fail if this is the
  346. * second jprobe at the address - two jprobes can't coexist
  347. */
  348. static int add_new_kprobe(struct kprobe *old_p, struct kprobe *p)
  349. {
  350. struct kprobe *kp;
  351. if (p->break_handler) {
  352. list_for_each_entry(kp, &old_p->list, list) {
  353. if (kp->break_handler)
  354. return -EEXIST;
  355. }
  356. list_add_tail(&p->list, &old_p->list);
  357. } else
  358. list_add(&p->list, &old_p->list);
  359. return 0;
  360. }
  361. /*
  362. * Fill in the required fields of the "manager kprobe". Replace the
  363. * earlier kprobe in the hlist with the manager kprobe
  364. */
  365. static inline void add_aggr_kprobe(struct kprobe *ap, struct kprobe *p)
  366. {
  367. copy_kprobe(p, ap);
  368. ap->addr = p->addr;
  369. ap->pre_handler = aggr_pre_handler;
  370. ap->post_handler = aggr_post_handler;
  371. ap->fault_handler = aggr_fault_handler;
  372. ap->break_handler = aggr_break_handler;
  373. INIT_LIST_HEAD(&ap->list);
  374. list_add(&p->list, &ap->list);
  375. INIT_HLIST_NODE(&ap->hlist);
  376. hlist_del(&p->hlist);
  377. hlist_add_head(&ap->hlist,
  378. &kprobe_table[hash_ptr(ap->addr, KPROBE_HASH_BITS)]);
  379. }
  380. /*
  381. * This is the second or subsequent kprobe at the address - handle
  382. * the intricacies
  383. * TODO: Move kcalloc outside the spinlock
  384. */
  385. static int register_aggr_kprobe(struct kprobe *old_p, struct kprobe *p)
  386. {
  387. int ret = 0;
  388. struct kprobe *ap;
  389. if (old_p->pre_handler == aggr_pre_handler) {
  390. copy_kprobe(old_p, p);
  391. ret = add_new_kprobe(old_p, p);
  392. } else {
  393. ap = kcalloc(1, sizeof(struct kprobe), GFP_ATOMIC);
  394. if (!ap)
  395. return -ENOMEM;
  396. add_aggr_kprobe(ap, old_p);
  397. copy_kprobe(ap, p);
  398. ret = add_new_kprobe(ap, p);
  399. }
  400. return ret;
  401. }
  402. /* kprobe removal house-keeping routines */
  403. static inline void cleanup_kprobe(struct kprobe *p, unsigned long flags)
  404. {
  405. arch_disarm_kprobe(p);
  406. hlist_del(&p->hlist);
  407. spin_unlock_irqrestore(&kprobe_lock, flags);
  408. arch_remove_kprobe(p);
  409. }
  410. static inline void cleanup_aggr_kprobe(struct kprobe *old_p,
  411. struct kprobe *p, unsigned long flags)
  412. {
  413. list_del(&p->list);
  414. if (list_empty(&old_p->list)) {
  415. cleanup_kprobe(old_p, flags);
  416. kfree(old_p);
  417. } else
  418. spin_unlock_irqrestore(&kprobe_lock, flags);
  419. }
  420. int register_kprobe(struct kprobe *p)
  421. {
  422. int ret = 0;
  423. unsigned long flags = 0;
  424. struct kprobe *old_p;
  425. if ((ret = arch_prepare_kprobe(p)) != 0) {
  426. goto rm_kprobe;
  427. }
  428. spin_lock_irqsave(&kprobe_lock, flags);
  429. old_p = get_kprobe(p->addr);
  430. p->nmissed = 0;
  431. if (old_p) {
  432. ret = register_aggr_kprobe(old_p, p);
  433. goto out;
  434. }
  435. arch_copy_kprobe(p);
  436. INIT_HLIST_NODE(&p->hlist);
  437. hlist_add_head(&p->hlist,
  438. &kprobe_table[hash_ptr(p->addr, KPROBE_HASH_BITS)]);
  439. arch_arm_kprobe(p);
  440. out:
  441. spin_unlock_irqrestore(&kprobe_lock, flags);
  442. rm_kprobe:
  443. if (ret == -EEXIST)
  444. arch_remove_kprobe(p);
  445. return ret;
  446. }
  447. void unregister_kprobe(struct kprobe *p)
  448. {
  449. unsigned long flags;
  450. struct kprobe *old_p;
  451. spin_lock_irqsave(&kprobe_lock, flags);
  452. old_p = get_kprobe(p->addr);
  453. if (old_p) {
  454. if (old_p->pre_handler == aggr_pre_handler)
  455. cleanup_aggr_kprobe(old_p, p, flags);
  456. else
  457. cleanup_kprobe(p, flags);
  458. } else
  459. spin_unlock_irqrestore(&kprobe_lock, flags);
  460. }
  461. static struct notifier_block kprobe_exceptions_nb = {
  462. .notifier_call = kprobe_exceptions_notify,
  463. .priority = 0x7fffffff /* we need to notified first */
  464. };
  465. int register_jprobe(struct jprobe *jp)
  466. {
  467. /* Todo: Verify probepoint is a function entry point */
  468. jp->kp.pre_handler = setjmp_pre_handler;
  469. jp->kp.break_handler = longjmp_break_handler;
  470. return register_kprobe(&jp->kp);
  471. }
  472. void unregister_jprobe(struct jprobe *jp)
  473. {
  474. unregister_kprobe(&jp->kp);
  475. }
  476. #ifdef ARCH_SUPPORTS_KRETPROBES
  477. int register_kretprobe(struct kretprobe *rp)
  478. {
  479. int ret = 0;
  480. struct kretprobe_instance *inst;
  481. int i;
  482. rp->kp.pre_handler = pre_handler_kretprobe;
  483. /* Pre-allocate memory for max kretprobe instances */
  484. if (rp->maxactive <= 0) {
  485. #ifdef CONFIG_PREEMPT
  486. rp->maxactive = max(10, 2 * NR_CPUS);
  487. #else
  488. rp->maxactive = NR_CPUS;
  489. #endif
  490. }
  491. INIT_HLIST_HEAD(&rp->used_instances);
  492. INIT_HLIST_HEAD(&rp->free_instances);
  493. for (i = 0; i < rp->maxactive; i++) {
  494. inst = kmalloc(sizeof(struct kretprobe_instance), GFP_KERNEL);
  495. if (inst == NULL) {
  496. free_rp_inst(rp);
  497. return -ENOMEM;
  498. }
  499. INIT_HLIST_NODE(&inst->uflist);
  500. hlist_add_head(&inst->uflist, &rp->free_instances);
  501. }
  502. rp->nmissed = 0;
  503. /* Establish function entry probe point */
  504. if ((ret = register_kprobe(&rp->kp)) != 0)
  505. free_rp_inst(rp);
  506. return ret;
  507. }
  508. #else /* ARCH_SUPPORTS_KRETPROBES */
  509. int register_kretprobe(struct kretprobe *rp)
  510. {
  511. return -ENOSYS;
  512. }
  513. #endif /* ARCH_SUPPORTS_KRETPROBES */
  514. void unregister_kretprobe(struct kretprobe *rp)
  515. {
  516. unsigned long flags;
  517. struct kretprobe_instance *ri;
  518. unregister_kprobe(&rp->kp);
  519. /* No race here */
  520. spin_lock_irqsave(&kprobe_lock, flags);
  521. free_rp_inst(rp);
  522. while ((ri = get_used_rp_inst(rp)) != NULL) {
  523. ri->rp = NULL;
  524. hlist_del(&ri->uflist);
  525. }
  526. spin_unlock_irqrestore(&kprobe_lock, flags);
  527. }
  528. static int __init init_kprobes(void)
  529. {
  530. int i, err = 0;
  531. /* FIXME allocate the probe table, currently defined statically */
  532. /* initialize all list heads */
  533. for (i = 0; i < KPROBE_TABLE_SIZE; i++) {
  534. INIT_HLIST_HEAD(&kprobe_table[i]);
  535. INIT_HLIST_HEAD(&kretprobe_inst_table[i]);
  536. }
  537. err = register_die_notifier(&kprobe_exceptions_nb);
  538. /* Register the trampoline probe for return probe */
  539. register_kprobe(&trampoline_p);
  540. return err;
  541. }
  542. __initcall(init_kprobes);
  543. EXPORT_SYMBOL_GPL(register_kprobe);
  544. EXPORT_SYMBOL_GPL(unregister_kprobe);
  545. EXPORT_SYMBOL_GPL(register_jprobe);
  546. EXPORT_SYMBOL_GPL(unregister_jprobe);
  547. EXPORT_SYMBOL_GPL(jprobe_return);
  548. EXPORT_SYMBOL_GPL(register_kretprobe);
  549. EXPORT_SYMBOL_GPL(unregister_kretprobe);