xmon.c 53 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560
  1. /*
  2. * Routines providing a simple monitor for use on the PowerMac.
  3. *
  4. * Copyright (C) 1996-2005 Paul Mackerras.
  5. *
  6. * This program is free software; you can redistribute it and/or
  7. * modify it under the terms of the GNU General Public License
  8. * as published by the Free Software Foundation; either version
  9. * 2 of the License, or (at your option) any later version.
  10. */
  11. #include <linux/config.h>
  12. #include <linux/errno.h>
  13. #include <linux/sched.h>
  14. #include <linux/smp.h>
  15. #include <linux/mm.h>
  16. #include <linux/reboot.h>
  17. #include <linux/delay.h>
  18. #include <linux/kallsyms.h>
  19. #include <linux/cpumask.h>
  20. #include <linux/module.h>
  21. #include <linux/sysrq.h>
  22. #include <asm/ptrace.h>
  23. #include <asm/string.h>
  24. #include <asm/prom.h>
  25. #include <asm/machdep.h>
  26. #include <asm/xmon.h>
  27. #ifdef CONFIG_PMAC_BACKLIGHT
  28. #include <asm/backlight.h>
  29. #endif
  30. #include <asm/processor.h>
  31. #include <asm/pgtable.h>
  32. #include <asm/mmu.h>
  33. #include <asm/mmu_context.h>
  34. #include <asm/cputable.h>
  35. #include <asm/rtas.h>
  36. #include <asm/sstep.h>
  37. #include <asm/bug.h>
  38. #ifdef CONFIG_PPC64
  39. #include <asm/hvcall.h>
  40. #include <asm/paca.h>
  41. #endif
  42. #include "nonstdio.h"
  43. #define scanhex xmon_scanhex
  44. #define skipbl xmon_skipbl
  45. #ifdef CONFIG_SMP
  46. cpumask_t cpus_in_xmon = CPU_MASK_NONE;
  47. static unsigned long xmon_taken = 1;
  48. static int xmon_owner;
  49. static int xmon_gate;
  50. #endif /* CONFIG_SMP */
  51. static unsigned long in_xmon = 0;
  52. static unsigned long adrs;
  53. static int size = 1;
  54. #define MAX_DUMP (128 * 1024)
  55. static unsigned long ndump = 64;
  56. static unsigned long nidump = 16;
  57. static unsigned long ncsum = 4096;
  58. static int termch;
  59. static char tmpstr[128];
  60. #define JMP_BUF_LEN 23
  61. static long bus_error_jmp[JMP_BUF_LEN];
  62. static int catch_memory_errors;
  63. static long *xmon_fault_jmp[NR_CPUS];
  64. #define setjmp xmon_setjmp
  65. #define longjmp xmon_longjmp
  66. /* Breakpoint stuff */
  67. struct bpt {
  68. unsigned long address;
  69. unsigned int instr[2];
  70. atomic_t ref_count;
  71. int enabled;
  72. unsigned long pad;
  73. };
  74. /* Bits in bpt.enabled */
  75. #define BP_IABR_TE 1 /* IABR translation enabled */
  76. #define BP_IABR 2
  77. #define BP_TRAP 8
  78. #define BP_DABR 0x10
  79. #define NBPTS 256
  80. static struct bpt bpts[NBPTS];
  81. static struct bpt dabr;
  82. static struct bpt *iabr;
  83. static unsigned bpinstr = 0x7fe00008; /* trap */
  84. #define BP_NUM(bp) ((bp) - bpts + 1)
  85. /* Prototypes */
  86. static int cmds(struct pt_regs *);
  87. static int mread(unsigned long, void *, int);
  88. static int mwrite(unsigned long, void *, int);
  89. static int handle_fault(struct pt_regs *);
  90. static void byterev(unsigned char *, int);
  91. static void memex(void);
  92. static int bsesc(void);
  93. static void dump(void);
  94. static void prdump(unsigned long, long);
  95. static int ppc_inst_dump(unsigned long, long, int);
  96. void print_address(unsigned long);
  97. static void backtrace(struct pt_regs *);
  98. static void excprint(struct pt_regs *);
  99. static void prregs(struct pt_regs *);
  100. static void memops(int);
  101. static void memlocate(void);
  102. static void memzcan(void);
  103. static void memdiffs(unsigned char *, unsigned char *, unsigned, unsigned);
  104. int skipbl(void);
  105. int scanhex(unsigned long *valp);
  106. static void scannl(void);
  107. static int hexdigit(int);
  108. void getstring(char *, int);
  109. static void flush_input(void);
  110. static int inchar(void);
  111. static void take_input(char *);
  112. static unsigned long read_spr(int);
  113. static void write_spr(int, unsigned long);
  114. static void super_regs(void);
  115. static void remove_bpts(void);
  116. static void insert_bpts(void);
  117. static void remove_cpu_bpts(void);
  118. static void insert_cpu_bpts(void);
  119. static struct bpt *at_breakpoint(unsigned long pc);
  120. static struct bpt *in_breakpoint_table(unsigned long pc, unsigned long *offp);
  121. static int do_step(struct pt_regs *);
  122. static void bpt_cmds(void);
  123. static void cacheflush(void);
  124. static int cpu_cmd(void);
  125. static void csum(void);
  126. static void bootcmds(void);
  127. static void proccall(void);
  128. void dump_segments(void);
  129. static void symbol_lookup(void);
  130. static void xmon_print_symbol(unsigned long address, const char *mid,
  131. const char *after);
  132. static const char *getvecname(unsigned long vec);
  133. extern int print_insn_powerpc(unsigned long, unsigned long, int);
  134. extern void xmon_enter(void);
  135. extern void xmon_leave(void);
  136. extern long setjmp(long *);
  137. extern void longjmp(long *, long);
  138. extern void xmon_save_regs(struct pt_regs *);
  139. #ifdef CONFIG_PPC64
  140. #define REG "%.16lx"
  141. #define REGS_PER_LINE 4
  142. #define LAST_VOLATILE 13
  143. #else
  144. #define REG "%.8lx"
  145. #define REGS_PER_LINE 8
  146. #define LAST_VOLATILE 12
  147. #endif
  148. #define GETWORD(v) (((v)[0] << 24) + ((v)[1] << 16) + ((v)[2] << 8) + (v)[3])
  149. #define isxdigit(c) (('0' <= (c) && (c) <= '9') \
  150. || ('a' <= (c) && (c) <= 'f') \
  151. || ('A' <= (c) && (c) <= 'F'))
  152. #define isalnum(c) (('0' <= (c) && (c) <= '9') \
  153. || ('a' <= (c) && (c) <= 'z') \
  154. || ('A' <= (c) && (c) <= 'Z'))
  155. #define isspace(c) (c == ' ' || c == '\t' || c == 10 || c == 13 || c == 0)
  156. static char *help_string = "\
  157. Commands:\n\
  158. b show breakpoints\n\
  159. bd set data breakpoint\n\
  160. bi set instruction breakpoint\n\
  161. bc clear breakpoint\n"
  162. #ifdef CONFIG_SMP
  163. "\
  164. c print cpus stopped in xmon\n\
  165. c# try to switch to cpu number h (in hex)\n"
  166. #endif
  167. "\
  168. C checksum\n\
  169. d dump bytes\n\
  170. di dump instructions\n\
  171. df dump float values\n\
  172. dd dump double values\n\
  173. e print exception information\n\
  174. f flush cache\n\
  175. la lookup symbol+offset of specified address\n\
  176. ls lookup address of specified symbol\n\
  177. m examine/change memory\n\
  178. mm move a block of memory\n\
  179. ms set a block of memory\n\
  180. md compare two blocks of memory\n\
  181. ml locate a block of memory\n\
  182. mz zero a block of memory\n\
  183. mi show information about memory allocation\n\
  184. p call a procedure\n\
  185. r print registers\n\
  186. s single step\n\
  187. S print special registers\n\
  188. t print backtrace\n\
  189. x exit monitor and recover\n\
  190. X exit monitor and dont recover\n"
  191. #ifdef CONFIG_PPC64
  192. " u dump segment table or SLB\n"
  193. #endif
  194. #ifdef CONFIG_PPC_STD_MMU_32
  195. " u dump segment registers\n"
  196. #endif
  197. " ? help\n"
  198. " zr reboot\n\
  199. zh halt\n"
  200. ;
  201. static struct pt_regs *xmon_regs;
  202. static inline void sync(void)
  203. {
  204. asm volatile("sync; isync");
  205. }
  206. static inline void store_inst(void *p)
  207. {
  208. asm volatile ("dcbst 0,%0; sync; icbi 0,%0; isync" : : "r" (p));
  209. }
  210. static inline void cflush(void *p)
  211. {
  212. asm volatile ("dcbf 0,%0; icbi 0,%0" : : "r" (p));
  213. }
  214. static inline void cinval(void *p)
  215. {
  216. asm volatile ("dcbi 0,%0; icbi 0,%0" : : "r" (p));
  217. }
  218. /*
  219. * Disable surveillance (the service processor watchdog function)
  220. * while we are in xmon.
  221. * XXX we should re-enable it when we leave. :)
  222. */
  223. #define SURVEILLANCE_TOKEN 9000
  224. static inline void disable_surveillance(void)
  225. {
  226. #ifdef CONFIG_PPC_PSERIES
  227. /* Since this can't be a module, args should end up below 4GB. */
  228. static struct rtas_args args;
  229. /*
  230. * At this point we have got all the cpus we can into
  231. * xmon, so there is hopefully no other cpu calling RTAS
  232. * at the moment, even though we don't take rtas.lock.
  233. * If we did try to take rtas.lock there would be a
  234. * real possibility of deadlock.
  235. */
  236. args.token = rtas_token("set-indicator");
  237. if (args.token == RTAS_UNKNOWN_SERVICE)
  238. return;
  239. args.nargs = 3;
  240. args.nret = 1;
  241. args.rets = &args.args[3];
  242. args.args[0] = SURVEILLANCE_TOKEN;
  243. args.args[1] = 0;
  244. args.args[2] = 0;
  245. enter_rtas(__pa(&args));
  246. #endif /* CONFIG_PPC_PSERIES */
  247. }
  248. #ifdef CONFIG_SMP
  249. static int xmon_speaker;
  250. static void get_output_lock(void)
  251. {
  252. int me = smp_processor_id() + 0x100;
  253. int last_speaker = 0, prev;
  254. long timeout;
  255. if (xmon_speaker == me)
  256. return;
  257. for (;;) {
  258. if (xmon_speaker == 0) {
  259. last_speaker = cmpxchg(&xmon_speaker, 0, me);
  260. if (last_speaker == 0)
  261. return;
  262. }
  263. timeout = 10000000;
  264. while (xmon_speaker == last_speaker) {
  265. if (--timeout > 0)
  266. continue;
  267. /* hostile takeover */
  268. prev = cmpxchg(&xmon_speaker, last_speaker, me);
  269. if (prev == last_speaker)
  270. return;
  271. break;
  272. }
  273. }
  274. }
  275. static void release_output_lock(void)
  276. {
  277. xmon_speaker = 0;
  278. }
  279. #endif
  280. int xmon_core(struct pt_regs *regs, int fromipi)
  281. {
  282. int cmd = 0;
  283. unsigned long msr;
  284. struct bpt *bp;
  285. long recurse_jmp[JMP_BUF_LEN];
  286. unsigned long offset;
  287. #ifdef CONFIG_SMP
  288. int cpu;
  289. int secondary;
  290. unsigned long timeout;
  291. #endif
  292. msr = mfmsr();
  293. mtmsr(msr & ~MSR_EE); /* disable interrupts */
  294. bp = in_breakpoint_table(regs->nip, &offset);
  295. if (bp != NULL) {
  296. regs->nip = bp->address + offset;
  297. atomic_dec(&bp->ref_count);
  298. }
  299. remove_cpu_bpts();
  300. #ifdef CONFIG_SMP
  301. cpu = smp_processor_id();
  302. if (cpu_isset(cpu, cpus_in_xmon)) {
  303. get_output_lock();
  304. excprint(regs);
  305. printf("cpu 0x%x: Exception %lx %s in xmon, "
  306. "returning to main loop\n",
  307. cpu, regs->trap, getvecname(TRAP(regs)));
  308. release_output_lock();
  309. longjmp(xmon_fault_jmp[cpu], 1);
  310. }
  311. if (setjmp(recurse_jmp) != 0) {
  312. if (!in_xmon || !xmon_gate) {
  313. get_output_lock();
  314. printf("xmon: WARNING: bad recursive fault "
  315. "on cpu 0x%x\n", cpu);
  316. release_output_lock();
  317. goto waiting;
  318. }
  319. secondary = !(xmon_taken && cpu == xmon_owner);
  320. goto cmdloop;
  321. }
  322. xmon_fault_jmp[cpu] = recurse_jmp;
  323. cpu_set(cpu, cpus_in_xmon);
  324. bp = NULL;
  325. if ((regs->msr & (MSR_IR|MSR_PR|MSR_SF)) == (MSR_IR|MSR_SF))
  326. bp = at_breakpoint(regs->nip);
  327. if (bp || (regs->msr & MSR_RI) == 0)
  328. fromipi = 0;
  329. if (!fromipi) {
  330. get_output_lock();
  331. excprint(regs);
  332. if (bp) {
  333. printf("cpu 0x%x stopped at breakpoint 0x%x (",
  334. cpu, BP_NUM(bp));
  335. xmon_print_symbol(regs->nip, " ", ")\n");
  336. }
  337. if ((regs->msr & MSR_RI) == 0)
  338. printf("WARNING: exception is not recoverable, "
  339. "can't continue\n");
  340. release_output_lock();
  341. }
  342. waiting:
  343. secondary = 1;
  344. while (secondary && !xmon_gate) {
  345. if (in_xmon == 0) {
  346. if (fromipi)
  347. goto leave;
  348. secondary = test_and_set_bit(0, &in_xmon);
  349. }
  350. barrier();
  351. }
  352. if (!secondary && !xmon_gate) {
  353. /* we are the first cpu to come in */
  354. /* interrupt other cpu(s) */
  355. int ncpus = num_online_cpus();
  356. xmon_owner = cpu;
  357. mb();
  358. if (ncpus > 1) {
  359. smp_send_debugger_break(MSG_ALL_BUT_SELF);
  360. /* wait for other cpus to come in */
  361. for (timeout = 100000000; timeout != 0; --timeout) {
  362. if (cpus_weight(cpus_in_xmon) >= ncpus)
  363. break;
  364. barrier();
  365. }
  366. }
  367. remove_bpts();
  368. disable_surveillance();
  369. /* for breakpoint or single step, print the current instr. */
  370. if (bp || TRAP(regs) == 0xd00)
  371. ppc_inst_dump(regs->nip, 1, 0);
  372. printf("enter ? for help\n");
  373. mb();
  374. xmon_gate = 1;
  375. barrier();
  376. }
  377. cmdloop:
  378. while (in_xmon) {
  379. if (secondary) {
  380. if (cpu == xmon_owner) {
  381. if (!test_and_set_bit(0, &xmon_taken)) {
  382. secondary = 0;
  383. continue;
  384. }
  385. /* missed it */
  386. while (cpu == xmon_owner)
  387. barrier();
  388. }
  389. barrier();
  390. } else {
  391. cmd = cmds(regs);
  392. if (cmd != 0) {
  393. /* exiting xmon */
  394. insert_bpts();
  395. xmon_gate = 0;
  396. wmb();
  397. in_xmon = 0;
  398. break;
  399. }
  400. /* have switched to some other cpu */
  401. secondary = 1;
  402. }
  403. }
  404. leave:
  405. cpu_clear(cpu, cpus_in_xmon);
  406. xmon_fault_jmp[cpu] = NULL;
  407. #else
  408. /* UP is simple... */
  409. if (in_xmon) {
  410. printf("Exception %lx %s in xmon, returning to main loop\n",
  411. regs->trap, getvecname(TRAP(regs)));
  412. longjmp(xmon_fault_jmp[0], 1);
  413. }
  414. if (setjmp(recurse_jmp) == 0) {
  415. xmon_fault_jmp[0] = recurse_jmp;
  416. in_xmon = 1;
  417. excprint(regs);
  418. bp = at_breakpoint(regs->nip);
  419. if (bp) {
  420. printf("Stopped at breakpoint %x (", BP_NUM(bp));
  421. xmon_print_symbol(regs->nip, " ", ")\n");
  422. }
  423. if ((regs->msr & MSR_RI) == 0)
  424. printf("WARNING: exception is not recoverable, "
  425. "can't continue\n");
  426. remove_bpts();
  427. disable_surveillance();
  428. /* for breakpoint or single step, print the current instr. */
  429. if (bp || TRAP(regs) == 0xd00)
  430. ppc_inst_dump(regs->nip, 1, 0);
  431. printf("enter ? for help\n");
  432. }
  433. cmd = cmds(regs);
  434. insert_bpts();
  435. in_xmon = 0;
  436. #endif
  437. if ((regs->msr & (MSR_IR|MSR_PR|MSR_SF)) == (MSR_IR|MSR_SF)) {
  438. bp = at_breakpoint(regs->nip);
  439. if (bp != NULL) {
  440. int stepped = emulate_step(regs, bp->instr[0]);
  441. if (stepped == 0) {
  442. regs->nip = (unsigned long) &bp->instr[0];
  443. atomic_inc(&bp->ref_count);
  444. } else if (stepped < 0) {
  445. printf("Couldn't single-step %s instruction\n",
  446. (IS_RFID(bp->instr[0])? "rfid": "mtmsrd"));
  447. }
  448. }
  449. }
  450. insert_cpu_bpts();
  451. mtmsr(msr); /* restore interrupt enable */
  452. return cmd != 'X';
  453. }
  454. int xmon(struct pt_regs *excp)
  455. {
  456. struct pt_regs regs;
  457. if (excp == NULL) {
  458. xmon_save_regs(&regs);
  459. excp = &regs;
  460. }
  461. return xmon_core(excp, 0);
  462. }
  463. EXPORT_SYMBOL(xmon);
  464. irqreturn_t
  465. xmon_irq(int irq, void *d, struct pt_regs *regs)
  466. {
  467. unsigned long flags;
  468. local_irq_save(flags);
  469. printf("Keyboard interrupt\n");
  470. xmon(regs);
  471. local_irq_restore(flags);
  472. return IRQ_HANDLED;
  473. }
  474. int xmon_bpt(struct pt_regs *regs)
  475. {
  476. struct bpt *bp;
  477. unsigned long offset;
  478. if ((regs->msr & (MSR_IR|MSR_PR|MSR_SF)) != (MSR_IR|MSR_SF))
  479. return 0;
  480. /* Are we at the trap at bp->instr[1] for some bp? */
  481. bp = in_breakpoint_table(regs->nip, &offset);
  482. if (bp != NULL && offset == 4) {
  483. regs->nip = bp->address + 4;
  484. atomic_dec(&bp->ref_count);
  485. return 1;
  486. }
  487. /* Are we at a breakpoint? */
  488. bp = at_breakpoint(regs->nip);
  489. if (!bp)
  490. return 0;
  491. xmon_core(regs, 0);
  492. return 1;
  493. }
  494. int xmon_sstep(struct pt_regs *regs)
  495. {
  496. if (user_mode(regs))
  497. return 0;
  498. xmon_core(regs, 0);
  499. return 1;
  500. }
  501. int xmon_dabr_match(struct pt_regs *regs)
  502. {
  503. if ((regs->msr & (MSR_IR|MSR_PR|MSR_SF)) != (MSR_IR|MSR_SF))
  504. return 0;
  505. if (dabr.enabled == 0)
  506. return 0;
  507. xmon_core(regs, 0);
  508. return 1;
  509. }
  510. int xmon_iabr_match(struct pt_regs *regs)
  511. {
  512. if ((regs->msr & (MSR_IR|MSR_PR|MSR_SF)) != (MSR_IR|MSR_SF))
  513. return 0;
  514. if (iabr == 0)
  515. return 0;
  516. xmon_core(regs, 0);
  517. return 1;
  518. }
  519. int xmon_ipi(struct pt_regs *regs)
  520. {
  521. #ifdef CONFIG_SMP
  522. if (in_xmon && !cpu_isset(smp_processor_id(), cpus_in_xmon))
  523. xmon_core(regs, 1);
  524. #endif
  525. return 0;
  526. }
  527. int xmon_fault_handler(struct pt_regs *regs)
  528. {
  529. struct bpt *bp;
  530. unsigned long offset;
  531. if (in_xmon && catch_memory_errors)
  532. handle_fault(regs); /* doesn't return */
  533. if ((regs->msr & (MSR_IR|MSR_PR|MSR_SF)) == (MSR_IR|MSR_SF)) {
  534. bp = in_breakpoint_table(regs->nip, &offset);
  535. if (bp != NULL) {
  536. regs->nip = bp->address + offset;
  537. atomic_dec(&bp->ref_count);
  538. }
  539. }
  540. return 0;
  541. }
  542. static struct bpt *at_breakpoint(unsigned long pc)
  543. {
  544. int i;
  545. struct bpt *bp;
  546. bp = bpts;
  547. for (i = 0; i < NBPTS; ++i, ++bp)
  548. if (bp->enabled && pc == bp->address)
  549. return bp;
  550. return NULL;
  551. }
  552. static struct bpt *in_breakpoint_table(unsigned long nip, unsigned long *offp)
  553. {
  554. unsigned long off;
  555. off = nip - (unsigned long) bpts;
  556. if (off >= sizeof(bpts))
  557. return NULL;
  558. off %= sizeof(struct bpt);
  559. if (off != offsetof(struct bpt, instr[0])
  560. && off != offsetof(struct bpt, instr[1]))
  561. return NULL;
  562. *offp = off - offsetof(struct bpt, instr[0]);
  563. return (struct bpt *) (nip - off);
  564. }
  565. static struct bpt *new_breakpoint(unsigned long a)
  566. {
  567. struct bpt *bp;
  568. a &= ~3UL;
  569. bp = at_breakpoint(a);
  570. if (bp)
  571. return bp;
  572. for (bp = bpts; bp < &bpts[NBPTS]; ++bp) {
  573. if (!bp->enabled && atomic_read(&bp->ref_count) == 0) {
  574. bp->address = a;
  575. bp->instr[1] = bpinstr;
  576. store_inst(&bp->instr[1]);
  577. return bp;
  578. }
  579. }
  580. printf("Sorry, no free breakpoints. Please clear one first.\n");
  581. return NULL;
  582. }
  583. static void insert_bpts(void)
  584. {
  585. int i;
  586. struct bpt *bp;
  587. bp = bpts;
  588. for (i = 0; i < NBPTS; ++i, ++bp) {
  589. if ((bp->enabled & (BP_TRAP|BP_IABR)) == 0)
  590. continue;
  591. if (mread(bp->address, &bp->instr[0], 4) != 4) {
  592. printf("Couldn't read instruction at %lx, "
  593. "disabling breakpoint there\n", bp->address);
  594. bp->enabled = 0;
  595. continue;
  596. }
  597. if (IS_MTMSRD(bp->instr[0]) || IS_RFID(bp->instr[0])) {
  598. printf("Breakpoint at %lx is on an mtmsrd or rfid "
  599. "instruction, disabling it\n", bp->address);
  600. bp->enabled = 0;
  601. continue;
  602. }
  603. store_inst(&bp->instr[0]);
  604. if (bp->enabled & BP_IABR)
  605. continue;
  606. if (mwrite(bp->address, &bpinstr, 4) != 4) {
  607. printf("Couldn't write instruction at %lx, "
  608. "disabling breakpoint there\n", bp->address);
  609. bp->enabled &= ~BP_TRAP;
  610. continue;
  611. }
  612. store_inst((void *)bp->address);
  613. }
  614. }
  615. static void insert_cpu_bpts(void)
  616. {
  617. if (dabr.enabled)
  618. set_dabr(dabr.address | (dabr.enabled & 7));
  619. if (iabr && cpu_has_feature(CPU_FTR_IABR))
  620. mtspr(SPRN_IABR, iabr->address
  621. | (iabr->enabled & (BP_IABR|BP_IABR_TE)));
  622. }
  623. static void remove_bpts(void)
  624. {
  625. int i;
  626. struct bpt *bp;
  627. unsigned instr;
  628. bp = bpts;
  629. for (i = 0; i < NBPTS; ++i, ++bp) {
  630. if ((bp->enabled & (BP_TRAP|BP_IABR)) != BP_TRAP)
  631. continue;
  632. if (mread(bp->address, &instr, 4) == 4
  633. && instr == bpinstr
  634. && mwrite(bp->address, &bp->instr, 4) != 4)
  635. printf("Couldn't remove breakpoint at %lx\n",
  636. bp->address);
  637. else
  638. store_inst((void *)bp->address);
  639. }
  640. }
  641. static void remove_cpu_bpts(void)
  642. {
  643. set_dabr(0);
  644. if (cpu_has_feature(CPU_FTR_IABR))
  645. mtspr(SPRN_IABR, 0);
  646. }
  647. /* Command interpreting routine */
  648. static char *last_cmd;
  649. static int
  650. cmds(struct pt_regs *excp)
  651. {
  652. int cmd = 0;
  653. last_cmd = NULL;
  654. xmon_regs = excp;
  655. for(;;) {
  656. #ifdef CONFIG_SMP
  657. printf("%x:", smp_processor_id());
  658. #endif /* CONFIG_SMP */
  659. printf("mon> ");
  660. flush_input();
  661. termch = 0;
  662. cmd = skipbl();
  663. if( cmd == '\n' ) {
  664. if (last_cmd == NULL)
  665. continue;
  666. take_input(last_cmd);
  667. last_cmd = NULL;
  668. cmd = inchar();
  669. }
  670. switch (cmd) {
  671. case 'm':
  672. cmd = inchar();
  673. switch (cmd) {
  674. case 'm':
  675. case 's':
  676. case 'd':
  677. memops(cmd);
  678. break;
  679. case 'l':
  680. memlocate();
  681. break;
  682. case 'z':
  683. memzcan();
  684. break;
  685. case 'i':
  686. show_mem();
  687. break;
  688. default:
  689. termch = cmd;
  690. memex();
  691. }
  692. break;
  693. case 'd':
  694. dump();
  695. break;
  696. case 'l':
  697. symbol_lookup();
  698. break;
  699. case 'r':
  700. prregs(excp); /* print regs */
  701. break;
  702. case 'e':
  703. excprint(excp);
  704. break;
  705. case 'S':
  706. super_regs();
  707. break;
  708. case 't':
  709. backtrace(excp);
  710. break;
  711. case 'f':
  712. cacheflush();
  713. break;
  714. case 's':
  715. if (do_step(excp))
  716. return cmd;
  717. break;
  718. case 'x':
  719. case 'X':
  720. case EOF:
  721. return cmd;
  722. case '?':
  723. printf(help_string);
  724. break;
  725. case 'b':
  726. bpt_cmds();
  727. break;
  728. case 'C':
  729. csum();
  730. break;
  731. case 'c':
  732. if (cpu_cmd())
  733. return 0;
  734. break;
  735. case 'z':
  736. bootcmds();
  737. break;
  738. case 'p':
  739. proccall();
  740. break;
  741. #ifdef CONFIG_PPC_STD_MMU
  742. case 'u':
  743. dump_segments();
  744. break;
  745. #endif
  746. default:
  747. printf("Unrecognized command: ");
  748. do {
  749. if (' ' < cmd && cmd <= '~')
  750. putchar(cmd);
  751. else
  752. printf("\\x%x", cmd);
  753. cmd = inchar();
  754. } while (cmd != '\n');
  755. printf(" (type ? for help)\n");
  756. break;
  757. }
  758. }
  759. }
  760. /*
  761. * Step a single instruction.
  762. * Some instructions we emulate, others we execute with MSR_SE set.
  763. */
  764. static int do_step(struct pt_regs *regs)
  765. {
  766. unsigned int instr;
  767. int stepped;
  768. /* check we are in 64-bit kernel mode, translation enabled */
  769. if ((regs->msr & (MSR_SF|MSR_PR|MSR_IR)) == (MSR_SF|MSR_IR)) {
  770. if (mread(regs->nip, &instr, 4) == 4) {
  771. stepped = emulate_step(regs, instr);
  772. if (stepped < 0) {
  773. printf("Couldn't single-step %s instruction\n",
  774. (IS_RFID(instr)? "rfid": "mtmsrd"));
  775. return 0;
  776. }
  777. if (stepped > 0) {
  778. regs->trap = 0xd00 | (regs->trap & 1);
  779. printf("stepped to ");
  780. xmon_print_symbol(regs->nip, " ", "\n");
  781. ppc_inst_dump(regs->nip, 1, 0);
  782. return 0;
  783. }
  784. }
  785. }
  786. regs->msr |= MSR_SE;
  787. return 1;
  788. }
  789. static void bootcmds(void)
  790. {
  791. int cmd;
  792. cmd = inchar();
  793. if (cmd == 'r')
  794. ppc_md.restart(NULL);
  795. else if (cmd == 'h')
  796. ppc_md.halt();
  797. else if (cmd == 'p')
  798. ppc_md.power_off();
  799. }
  800. static int cpu_cmd(void)
  801. {
  802. #ifdef CONFIG_SMP
  803. unsigned long cpu;
  804. int timeout;
  805. int count;
  806. if (!scanhex(&cpu)) {
  807. /* print cpus waiting or in xmon */
  808. printf("cpus stopped:");
  809. count = 0;
  810. for (cpu = 0; cpu < NR_CPUS; ++cpu) {
  811. if (cpu_isset(cpu, cpus_in_xmon)) {
  812. if (count == 0)
  813. printf(" %x", cpu);
  814. ++count;
  815. } else {
  816. if (count > 1)
  817. printf("-%x", cpu - 1);
  818. count = 0;
  819. }
  820. }
  821. if (count > 1)
  822. printf("-%x", NR_CPUS - 1);
  823. printf("\n");
  824. return 0;
  825. }
  826. /* try to switch to cpu specified */
  827. if (!cpu_isset(cpu, cpus_in_xmon)) {
  828. printf("cpu 0x%x isn't in xmon\n", cpu);
  829. return 0;
  830. }
  831. xmon_taken = 0;
  832. mb();
  833. xmon_owner = cpu;
  834. timeout = 10000000;
  835. while (!xmon_taken) {
  836. if (--timeout == 0) {
  837. if (test_and_set_bit(0, &xmon_taken))
  838. break;
  839. /* take control back */
  840. mb();
  841. xmon_owner = smp_processor_id();
  842. printf("cpu %u didn't take control\n", cpu);
  843. return 0;
  844. }
  845. barrier();
  846. }
  847. return 1;
  848. #else
  849. return 0;
  850. #endif /* CONFIG_SMP */
  851. }
  852. static unsigned short fcstab[256] = {
  853. 0x0000, 0x1189, 0x2312, 0x329b, 0x4624, 0x57ad, 0x6536, 0x74bf,
  854. 0x8c48, 0x9dc1, 0xaf5a, 0xbed3, 0xca6c, 0xdbe5, 0xe97e, 0xf8f7,
  855. 0x1081, 0x0108, 0x3393, 0x221a, 0x56a5, 0x472c, 0x75b7, 0x643e,
  856. 0x9cc9, 0x8d40, 0xbfdb, 0xae52, 0xdaed, 0xcb64, 0xf9ff, 0xe876,
  857. 0x2102, 0x308b, 0x0210, 0x1399, 0x6726, 0x76af, 0x4434, 0x55bd,
  858. 0xad4a, 0xbcc3, 0x8e58, 0x9fd1, 0xeb6e, 0xfae7, 0xc87c, 0xd9f5,
  859. 0x3183, 0x200a, 0x1291, 0x0318, 0x77a7, 0x662e, 0x54b5, 0x453c,
  860. 0xbdcb, 0xac42, 0x9ed9, 0x8f50, 0xfbef, 0xea66, 0xd8fd, 0xc974,
  861. 0x4204, 0x538d, 0x6116, 0x709f, 0x0420, 0x15a9, 0x2732, 0x36bb,
  862. 0xce4c, 0xdfc5, 0xed5e, 0xfcd7, 0x8868, 0x99e1, 0xab7a, 0xbaf3,
  863. 0x5285, 0x430c, 0x7197, 0x601e, 0x14a1, 0x0528, 0x37b3, 0x263a,
  864. 0xdecd, 0xcf44, 0xfddf, 0xec56, 0x98e9, 0x8960, 0xbbfb, 0xaa72,
  865. 0x6306, 0x728f, 0x4014, 0x519d, 0x2522, 0x34ab, 0x0630, 0x17b9,
  866. 0xef4e, 0xfec7, 0xcc5c, 0xddd5, 0xa96a, 0xb8e3, 0x8a78, 0x9bf1,
  867. 0x7387, 0x620e, 0x5095, 0x411c, 0x35a3, 0x242a, 0x16b1, 0x0738,
  868. 0xffcf, 0xee46, 0xdcdd, 0xcd54, 0xb9eb, 0xa862, 0x9af9, 0x8b70,
  869. 0x8408, 0x9581, 0xa71a, 0xb693, 0xc22c, 0xd3a5, 0xe13e, 0xf0b7,
  870. 0x0840, 0x19c9, 0x2b52, 0x3adb, 0x4e64, 0x5fed, 0x6d76, 0x7cff,
  871. 0x9489, 0x8500, 0xb79b, 0xa612, 0xd2ad, 0xc324, 0xf1bf, 0xe036,
  872. 0x18c1, 0x0948, 0x3bd3, 0x2a5a, 0x5ee5, 0x4f6c, 0x7df7, 0x6c7e,
  873. 0xa50a, 0xb483, 0x8618, 0x9791, 0xe32e, 0xf2a7, 0xc03c, 0xd1b5,
  874. 0x2942, 0x38cb, 0x0a50, 0x1bd9, 0x6f66, 0x7eef, 0x4c74, 0x5dfd,
  875. 0xb58b, 0xa402, 0x9699, 0x8710, 0xf3af, 0xe226, 0xd0bd, 0xc134,
  876. 0x39c3, 0x284a, 0x1ad1, 0x0b58, 0x7fe7, 0x6e6e, 0x5cf5, 0x4d7c,
  877. 0xc60c, 0xd785, 0xe51e, 0xf497, 0x8028, 0x91a1, 0xa33a, 0xb2b3,
  878. 0x4a44, 0x5bcd, 0x6956, 0x78df, 0x0c60, 0x1de9, 0x2f72, 0x3efb,
  879. 0xd68d, 0xc704, 0xf59f, 0xe416, 0x90a9, 0x8120, 0xb3bb, 0xa232,
  880. 0x5ac5, 0x4b4c, 0x79d7, 0x685e, 0x1ce1, 0x0d68, 0x3ff3, 0x2e7a,
  881. 0xe70e, 0xf687, 0xc41c, 0xd595, 0xa12a, 0xb0a3, 0x8238, 0x93b1,
  882. 0x6b46, 0x7acf, 0x4854, 0x59dd, 0x2d62, 0x3ceb, 0x0e70, 0x1ff9,
  883. 0xf78f, 0xe606, 0xd49d, 0xc514, 0xb1ab, 0xa022, 0x92b9, 0x8330,
  884. 0x7bc7, 0x6a4e, 0x58d5, 0x495c, 0x3de3, 0x2c6a, 0x1ef1, 0x0f78
  885. };
  886. #define FCS(fcs, c) (((fcs) >> 8) ^ fcstab[((fcs) ^ (c)) & 0xff])
  887. static void
  888. csum(void)
  889. {
  890. unsigned int i;
  891. unsigned short fcs;
  892. unsigned char v;
  893. if (!scanhex(&adrs))
  894. return;
  895. if (!scanhex(&ncsum))
  896. return;
  897. fcs = 0xffff;
  898. for (i = 0; i < ncsum; ++i) {
  899. if (mread(adrs+i, &v, 1) == 0) {
  900. printf("csum stopped at %x\n", adrs+i);
  901. break;
  902. }
  903. fcs = FCS(fcs, v);
  904. }
  905. printf("%x\n", fcs);
  906. }
  907. /*
  908. * Check if this is a suitable place to put a breakpoint.
  909. */
  910. static long check_bp_loc(unsigned long addr)
  911. {
  912. unsigned int instr;
  913. addr &= ~3;
  914. if (addr < KERNELBASE) {
  915. printf("Breakpoints may only be placed at kernel addresses\n");
  916. return 0;
  917. }
  918. if (!mread(addr, &instr, sizeof(instr))) {
  919. printf("Can't read instruction at address %lx\n", addr);
  920. return 0;
  921. }
  922. if (IS_MTMSRD(instr) || IS_RFID(instr)) {
  923. printf("Breakpoints may not be placed on mtmsrd or rfid "
  924. "instructions\n");
  925. return 0;
  926. }
  927. return 1;
  928. }
  929. static char *breakpoint_help_string =
  930. "Breakpoint command usage:\n"
  931. "b show breakpoints\n"
  932. "b <addr> [cnt] set breakpoint at given instr addr\n"
  933. "bc clear all breakpoints\n"
  934. "bc <n/addr> clear breakpoint number n or at addr\n"
  935. "bi <addr> [cnt] set hardware instr breakpoint (POWER3/RS64 only)\n"
  936. "bd <addr> [cnt] set hardware data breakpoint\n"
  937. "";
  938. static void
  939. bpt_cmds(void)
  940. {
  941. int cmd;
  942. unsigned long a;
  943. int mode, i;
  944. struct bpt *bp;
  945. const char badaddr[] = "Only kernel addresses are permitted "
  946. "for breakpoints\n";
  947. cmd = inchar();
  948. switch (cmd) {
  949. #ifndef CONFIG_8xx
  950. case 'd': /* bd - hardware data breakpoint */
  951. mode = 7;
  952. cmd = inchar();
  953. if (cmd == 'r')
  954. mode = 5;
  955. else if (cmd == 'w')
  956. mode = 6;
  957. else
  958. termch = cmd;
  959. dabr.address = 0;
  960. dabr.enabled = 0;
  961. if (scanhex(&dabr.address)) {
  962. if (dabr.address < KERNELBASE) {
  963. printf(badaddr);
  964. break;
  965. }
  966. dabr.address &= ~7;
  967. dabr.enabled = mode | BP_DABR;
  968. }
  969. break;
  970. case 'i': /* bi - hardware instr breakpoint */
  971. if (!cpu_has_feature(CPU_FTR_IABR)) {
  972. printf("Hardware instruction breakpoint "
  973. "not supported on this cpu\n");
  974. break;
  975. }
  976. if (iabr) {
  977. iabr->enabled &= ~(BP_IABR | BP_IABR_TE);
  978. iabr = NULL;
  979. }
  980. if (!scanhex(&a))
  981. break;
  982. if (!check_bp_loc(a))
  983. break;
  984. bp = new_breakpoint(a);
  985. if (bp != NULL) {
  986. bp->enabled |= BP_IABR | BP_IABR_TE;
  987. iabr = bp;
  988. }
  989. break;
  990. #endif
  991. case 'c':
  992. if (!scanhex(&a)) {
  993. /* clear all breakpoints */
  994. for (i = 0; i < NBPTS; ++i)
  995. bpts[i].enabled = 0;
  996. iabr = NULL;
  997. dabr.enabled = 0;
  998. printf("All breakpoints cleared\n");
  999. break;
  1000. }
  1001. if (a <= NBPTS && a >= 1) {
  1002. /* assume a breakpoint number */
  1003. bp = &bpts[a-1]; /* bp nums are 1 based */
  1004. } else {
  1005. /* assume a breakpoint address */
  1006. bp = at_breakpoint(a);
  1007. if (bp == 0) {
  1008. printf("No breakpoint at %x\n", a);
  1009. break;
  1010. }
  1011. }
  1012. printf("Cleared breakpoint %x (", BP_NUM(bp));
  1013. xmon_print_symbol(bp->address, " ", ")\n");
  1014. bp->enabled = 0;
  1015. break;
  1016. default:
  1017. termch = cmd;
  1018. cmd = skipbl();
  1019. if (cmd == '?') {
  1020. printf(breakpoint_help_string);
  1021. break;
  1022. }
  1023. termch = cmd;
  1024. if (!scanhex(&a)) {
  1025. /* print all breakpoints */
  1026. printf(" type address\n");
  1027. if (dabr.enabled) {
  1028. printf(" data "REG" [", dabr.address);
  1029. if (dabr.enabled & 1)
  1030. printf("r");
  1031. if (dabr.enabled & 2)
  1032. printf("w");
  1033. printf("]\n");
  1034. }
  1035. for (bp = bpts; bp < &bpts[NBPTS]; ++bp) {
  1036. if (!bp->enabled)
  1037. continue;
  1038. printf("%2x %s ", BP_NUM(bp),
  1039. (bp->enabled & BP_IABR)? "inst": "trap");
  1040. xmon_print_symbol(bp->address, " ", "\n");
  1041. }
  1042. break;
  1043. }
  1044. if (!check_bp_loc(a))
  1045. break;
  1046. bp = new_breakpoint(a);
  1047. if (bp != NULL)
  1048. bp->enabled |= BP_TRAP;
  1049. break;
  1050. }
  1051. }
  1052. /* Very cheap human name for vector lookup. */
  1053. static
  1054. const char *getvecname(unsigned long vec)
  1055. {
  1056. char *ret;
  1057. switch (vec) {
  1058. case 0x100: ret = "(System Reset)"; break;
  1059. case 0x200: ret = "(Machine Check)"; break;
  1060. case 0x300: ret = "(Data Access)"; break;
  1061. case 0x380: ret = "(Data SLB Access)"; break;
  1062. case 0x400: ret = "(Instruction Access)"; break;
  1063. case 0x480: ret = "(Instruction SLB Access)"; break;
  1064. case 0x500: ret = "(Hardware Interrupt)"; break;
  1065. case 0x600: ret = "(Alignment)"; break;
  1066. case 0x700: ret = "(Program Check)"; break;
  1067. case 0x800: ret = "(FPU Unavailable)"; break;
  1068. case 0x900: ret = "(Decrementer)"; break;
  1069. case 0xc00: ret = "(System Call)"; break;
  1070. case 0xd00: ret = "(Single Step)"; break;
  1071. case 0xf00: ret = "(Performance Monitor)"; break;
  1072. case 0xf20: ret = "(Altivec Unavailable)"; break;
  1073. case 0x1300: ret = "(Instruction Breakpoint)"; break;
  1074. default: ret = "";
  1075. }
  1076. return ret;
  1077. }
  1078. static void get_function_bounds(unsigned long pc, unsigned long *startp,
  1079. unsigned long *endp)
  1080. {
  1081. unsigned long size, offset;
  1082. const char *name;
  1083. char *modname;
  1084. *startp = *endp = 0;
  1085. if (pc == 0)
  1086. return;
  1087. if (setjmp(bus_error_jmp) == 0) {
  1088. catch_memory_errors = 1;
  1089. sync();
  1090. name = kallsyms_lookup(pc, &size, &offset, &modname, tmpstr);
  1091. if (name != NULL) {
  1092. *startp = pc - offset;
  1093. *endp = pc - offset + size;
  1094. }
  1095. sync();
  1096. }
  1097. catch_memory_errors = 0;
  1098. }
  1099. static int xmon_depth_to_print = 64;
  1100. #ifdef CONFIG_PPC64
  1101. #define LRSAVE_OFFSET 0x10
  1102. #define REG_FRAME_MARKER 0x7265677368657265ul /* "regshere" */
  1103. #define MARKER_OFFSET 0x60
  1104. #define REGS_OFFSET 0x70
  1105. #else
  1106. #define LRSAVE_OFFSET 4
  1107. #define REG_FRAME_MARKER 0x72656773
  1108. #define MARKER_OFFSET 8
  1109. #define REGS_OFFSET 16
  1110. #endif
  1111. static void xmon_show_stack(unsigned long sp, unsigned long lr,
  1112. unsigned long pc)
  1113. {
  1114. unsigned long ip;
  1115. unsigned long newsp;
  1116. unsigned long marker;
  1117. int count = 0;
  1118. struct pt_regs regs;
  1119. do {
  1120. if (sp < PAGE_OFFSET) {
  1121. if (sp != 0)
  1122. printf("SP (%lx) is in userspace\n", sp);
  1123. break;
  1124. }
  1125. if (!mread(sp + LRSAVE_OFFSET, &ip, sizeof(unsigned long))
  1126. || !mread(sp, &newsp, sizeof(unsigned long))) {
  1127. printf("Couldn't read stack frame at %lx\n", sp);
  1128. break;
  1129. }
  1130. /*
  1131. * For the first stack frame, try to work out if
  1132. * LR and/or the saved LR value in the bottommost
  1133. * stack frame are valid.
  1134. */
  1135. if ((pc | lr) != 0) {
  1136. unsigned long fnstart, fnend;
  1137. unsigned long nextip;
  1138. int printip = 1;
  1139. get_function_bounds(pc, &fnstart, &fnend);
  1140. nextip = 0;
  1141. if (newsp > sp)
  1142. mread(newsp + LRSAVE_OFFSET, &nextip,
  1143. sizeof(unsigned long));
  1144. if (lr == ip) {
  1145. if (lr < PAGE_OFFSET
  1146. || (fnstart <= lr && lr < fnend))
  1147. printip = 0;
  1148. } else if (lr == nextip) {
  1149. printip = 0;
  1150. } else if (lr >= PAGE_OFFSET
  1151. && !(fnstart <= lr && lr < fnend)) {
  1152. printf("[link register ] ");
  1153. xmon_print_symbol(lr, " ", "\n");
  1154. }
  1155. if (printip) {
  1156. printf("["REG"] ", sp);
  1157. xmon_print_symbol(ip, " ", " (unreliable)\n");
  1158. }
  1159. pc = lr = 0;
  1160. } else {
  1161. printf("["REG"] ", sp);
  1162. xmon_print_symbol(ip, " ", "\n");
  1163. }
  1164. /* Look for "regshere" marker to see if this is
  1165. an exception frame. */
  1166. if (mread(sp + MARKER_OFFSET, &marker, sizeof(unsigned long))
  1167. && marker == REG_FRAME_MARKER) {
  1168. if (mread(sp + REGS_OFFSET, &regs, sizeof(regs))
  1169. != sizeof(regs)) {
  1170. printf("Couldn't read registers at %lx\n",
  1171. sp + REGS_OFFSET);
  1172. break;
  1173. }
  1174. printf("--- Exception: %lx %s at ", regs.trap,
  1175. getvecname(TRAP(&regs)));
  1176. pc = regs.nip;
  1177. lr = regs.link;
  1178. xmon_print_symbol(pc, " ", "\n");
  1179. }
  1180. if (newsp == 0)
  1181. break;
  1182. sp = newsp;
  1183. } while (count++ < xmon_depth_to_print);
  1184. }
  1185. static void backtrace(struct pt_regs *excp)
  1186. {
  1187. unsigned long sp;
  1188. if (scanhex(&sp))
  1189. xmon_show_stack(sp, 0, 0);
  1190. else
  1191. xmon_show_stack(excp->gpr[1], excp->link, excp->nip);
  1192. scannl();
  1193. }
  1194. static void print_bug_trap(struct pt_regs *regs)
  1195. {
  1196. struct bug_entry *bug;
  1197. unsigned long addr;
  1198. if (regs->msr & MSR_PR)
  1199. return; /* not in kernel */
  1200. addr = regs->nip; /* address of trap instruction */
  1201. if (addr < PAGE_OFFSET)
  1202. return;
  1203. bug = find_bug(regs->nip);
  1204. if (bug == NULL)
  1205. return;
  1206. if (bug->line & BUG_WARNING_TRAP)
  1207. return;
  1208. printf("kernel BUG in %s at %s:%d!\n",
  1209. bug->function, bug->file, (unsigned int)bug->line);
  1210. }
  1211. void excprint(struct pt_regs *fp)
  1212. {
  1213. unsigned long trap;
  1214. #ifdef CONFIG_SMP
  1215. printf("cpu 0x%x: ", smp_processor_id());
  1216. #endif /* CONFIG_SMP */
  1217. trap = TRAP(fp);
  1218. printf("Vector: %lx %s at [%lx]\n", fp->trap, getvecname(trap), fp);
  1219. printf(" pc: ");
  1220. xmon_print_symbol(fp->nip, ": ", "\n");
  1221. printf(" lr: ", fp->link);
  1222. xmon_print_symbol(fp->link, ": ", "\n");
  1223. printf(" sp: %lx\n", fp->gpr[1]);
  1224. printf(" msr: %lx\n", fp->msr);
  1225. if (trap == 0x300 || trap == 0x380 || trap == 0x600) {
  1226. printf(" dar: %lx\n", fp->dar);
  1227. if (trap != 0x380)
  1228. printf(" dsisr: %lx\n", fp->dsisr);
  1229. }
  1230. printf(" current = 0x%lx\n", current);
  1231. #ifdef CONFIG_PPC64
  1232. printf(" paca = 0x%lx\n", get_paca());
  1233. #endif
  1234. if (current) {
  1235. printf(" pid = %ld, comm = %s\n",
  1236. current->pid, current->comm);
  1237. }
  1238. if (trap == 0x700)
  1239. print_bug_trap(fp);
  1240. }
  1241. void prregs(struct pt_regs *fp)
  1242. {
  1243. int n, trap;
  1244. unsigned long base;
  1245. struct pt_regs regs;
  1246. if (scanhex(&base)) {
  1247. if (setjmp(bus_error_jmp) == 0) {
  1248. catch_memory_errors = 1;
  1249. sync();
  1250. regs = *(struct pt_regs *)base;
  1251. sync();
  1252. __delay(200);
  1253. } else {
  1254. catch_memory_errors = 0;
  1255. printf("*** Error reading registers from "REG"\n",
  1256. base);
  1257. return;
  1258. }
  1259. catch_memory_errors = 0;
  1260. fp = &regs;
  1261. }
  1262. #ifdef CONFIG_PPC64
  1263. if (FULL_REGS(fp)) {
  1264. for (n = 0; n < 16; ++n)
  1265. printf("R%.2ld = "REG" R%.2ld = "REG"\n",
  1266. n, fp->gpr[n], n+16, fp->gpr[n+16]);
  1267. } else {
  1268. for (n = 0; n < 7; ++n)
  1269. printf("R%.2ld = "REG" R%.2ld = "REG"\n",
  1270. n, fp->gpr[n], n+7, fp->gpr[n+7]);
  1271. }
  1272. #else
  1273. for (n = 0; n < 32; ++n) {
  1274. printf("R%.2d = %.8x%s", n, fp->gpr[n],
  1275. (n & 3) == 3? "\n": " ");
  1276. if (n == 12 && !FULL_REGS(fp)) {
  1277. printf("\n");
  1278. break;
  1279. }
  1280. }
  1281. #endif
  1282. printf("pc = ");
  1283. xmon_print_symbol(fp->nip, " ", "\n");
  1284. printf("lr = ");
  1285. xmon_print_symbol(fp->link, " ", "\n");
  1286. printf("msr = "REG" cr = %.8lx\n", fp->msr, fp->ccr);
  1287. printf("ctr = "REG" xer = "REG" trap = %4lx\n",
  1288. fp->ctr, fp->xer, fp->trap);
  1289. trap = TRAP(fp);
  1290. if (trap == 0x300 || trap == 0x380 || trap == 0x600)
  1291. printf("dar = "REG" dsisr = %.8lx\n", fp->dar, fp->dsisr);
  1292. }
  1293. void cacheflush(void)
  1294. {
  1295. int cmd;
  1296. unsigned long nflush;
  1297. cmd = inchar();
  1298. if (cmd != 'i')
  1299. termch = cmd;
  1300. scanhex((void *)&adrs);
  1301. if (termch != '\n')
  1302. termch = 0;
  1303. nflush = 1;
  1304. scanhex(&nflush);
  1305. nflush = (nflush + L1_CACHE_BYTES - 1) / L1_CACHE_BYTES;
  1306. if (setjmp(bus_error_jmp) == 0) {
  1307. catch_memory_errors = 1;
  1308. sync();
  1309. if (cmd != 'i') {
  1310. for (; nflush > 0; --nflush, adrs += L1_CACHE_BYTES)
  1311. cflush((void *) adrs);
  1312. } else {
  1313. for (; nflush > 0; --nflush, adrs += L1_CACHE_BYTES)
  1314. cinval((void *) adrs);
  1315. }
  1316. sync();
  1317. /* wait a little while to see if we get a machine check */
  1318. __delay(200);
  1319. }
  1320. catch_memory_errors = 0;
  1321. }
  1322. unsigned long
  1323. read_spr(int n)
  1324. {
  1325. unsigned int instrs[2];
  1326. unsigned long (*code)(void);
  1327. unsigned long ret = -1UL;
  1328. #ifdef CONFIG_PPC64
  1329. unsigned long opd[3];
  1330. opd[0] = (unsigned long)instrs;
  1331. opd[1] = 0;
  1332. opd[2] = 0;
  1333. code = (unsigned long (*)(void)) opd;
  1334. #else
  1335. code = (unsigned long (*)(void)) instrs;
  1336. #endif
  1337. /* mfspr r3,n; blr */
  1338. instrs[0] = 0x7c6002a6 + ((n & 0x1F) << 16) + ((n & 0x3e0) << 6);
  1339. instrs[1] = 0x4e800020;
  1340. store_inst(instrs);
  1341. store_inst(instrs+1);
  1342. if (setjmp(bus_error_jmp) == 0) {
  1343. catch_memory_errors = 1;
  1344. sync();
  1345. ret = code();
  1346. sync();
  1347. /* wait a little while to see if we get a machine check */
  1348. __delay(200);
  1349. n = size;
  1350. }
  1351. return ret;
  1352. }
  1353. void
  1354. write_spr(int n, unsigned long val)
  1355. {
  1356. unsigned int instrs[2];
  1357. unsigned long (*code)(unsigned long);
  1358. #ifdef CONFIG_PPC64
  1359. unsigned long opd[3];
  1360. opd[0] = (unsigned long)instrs;
  1361. opd[1] = 0;
  1362. opd[2] = 0;
  1363. code = (unsigned long (*)(unsigned long)) opd;
  1364. #else
  1365. code = (unsigned long (*)(unsigned long)) instrs;
  1366. #endif
  1367. instrs[0] = 0x7c6003a6 + ((n & 0x1F) << 16) + ((n & 0x3e0) << 6);
  1368. instrs[1] = 0x4e800020;
  1369. store_inst(instrs);
  1370. store_inst(instrs+1);
  1371. if (setjmp(bus_error_jmp) == 0) {
  1372. catch_memory_errors = 1;
  1373. sync();
  1374. code(val);
  1375. sync();
  1376. /* wait a little while to see if we get a machine check */
  1377. __delay(200);
  1378. n = size;
  1379. }
  1380. }
  1381. static unsigned long regno;
  1382. extern char exc_prolog;
  1383. extern char dec_exc;
  1384. void super_regs(void)
  1385. {
  1386. int cmd;
  1387. unsigned long val;
  1388. #ifdef CONFIG_PPC_ISERIES
  1389. struct paca_struct *ptrPaca = NULL;
  1390. struct lppaca *ptrLpPaca = NULL;
  1391. struct ItLpRegSave *ptrLpRegSave = NULL;
  1392. #endif
  1393. cmd = skipbl();
  1394. if (cmd == '\n') {
  1395. unsigned long sp, toc;
  1396. asm("mr %0,1" : "=r" (sp) :);
  1397. asm("mr %0,2" : "=r" (toc) :);
  1398. printf("msr = "REG" sprg0= "REG"\n",
  1399. mfmsr(), mfspr(SPRN_SPRG0));
  1400. printf("pvr = "REG" sprg1= "REG"\n",
  1401. mfspr(SPRN_PVR), mfspr(SPRN_SPRG1));
  1402. printf("dec = "REG" sprg2= "REG"\n",
  1403. mfspr(SPRN_DEC), mfspr(SPRN_SPRG2));
  1404. printf("sp = "REG" sprg3= "REG"\n", sp, mfspr(SPRN_SPRG3));
  1405. printf("toc = "REG" dar = "REG"\n", toc, mfspr(SPRN_DAR));
  1406. #ifdef CONFIG_PPC_ISERIES
  1407. // Dump out relevant Paca data areas.
  1408. printf("Paca: \n");
  1409. ptrPaca = get_paca();
  1410. printf(" Local Processor Control Area (LpPaca): \n");
  1411. ptrLpPaca = ptrPaca->lppaca_ptr;
  1412. printf(" Saved Srr0=%.16lx Saved Srr1=%.16lx \n",
  1413. ptrLpPaca->saved_srr0, ptrLpPaca->saved_srr1);
  1414. printf(" Saved Gpr3=%.16lx Saved Gpr4=%.16lx \n",
  1415. ptrLpPaca->saved_gpr3, ptrLpPaca->saved_gpr4);
  1416. printf(" Saved Gpr5=%.16lx \n", ptrLpPaca->saved_gpr5);
  1417. printf(" Local Processor Register Save Area (LpRegSave): \n");
  1418. ptrLpRegSave = ptrPaca->reg_save_ptr;
  1419. printf(" Saved Sprg0=%.16lx Saved Sprg1=%.16lx \n",
  1420. ptrLpRegSave->xSPRG0, ptrLpRegSave->xSPRG0);
  1421. printf(" Saved Sprg2=%.16lx Saved Sprg3=%.16lx \n",
  1422. ptrLpRegSave->xSPRG2, ptrLpRegSave->xSPRG3);
  1423. printf(" Saved Msr =%.16lx Saved Nia =%.16lx \n",
  1424. ptrLpRegSave->xMSR, ptrLpRegSave->xNIA);
  1425. #endif
  1426. return;
  1427. }
  1428. scanhex(&regno);
  1429. switch (cmd) {
  1430. case 'w':
  1431. val = read_spr(regno);
  1432. scanhex(&val);
  1433. write_spr(regno, val);
  1434. /* fall through */
  1435. case 'r':
  1436. printf("spr %lx = %lx\n", regno, read_spr(regno));
  1437. break;
  1438. }
  1439. scannl();
  1440. }
  1441. /*
  1442. * Stuff for reading and writing memory safely
  1443. */
  1444. int
  1445. mread(unsigned long adrs, void *buf, int size)
  1446. {
  1447. volatile int n;
  1448. char *p, *q;
  1449. n = 0;
  1450. if (setjmp(bus_error_jmp) == 0) {
  1451. catch_memory_errors = 1;
  1452. sync();
  1453. p = (char *)adrs;
  1454. q = (char *)buf;
  1455. switch (size) {
  1456. case 2:
  1457. *(u16 *)q = *(u16 *)p;
  1458. break;
  1459. case 4:
  1460. *(u32 *)q = *(u32 *)p;
  1461. break;
  1462. case 8:
  1463. *(u64 *)q = *(u64 *)p;
  1464. break;
  1465. default:
  1466. for( ; n < size; ++n) {
  1467. *q++ = *p++;
  1468. sync();
  1469. }
  1470. }
  1471. sync();
  1472. /* wait a little while to see if we get a machine check */
  1473. __delay(200);
  1474. n = size;
  1475. }
  1476. catch_memory_errors = 0;
  1477. return n;
  1478. }
  1479. int
  1480. mwrite(unsigned long adrs, void *buf, int size)
  1481. {
  1482. volatile int n;
  1483. char *p, *q;
  1484. n = 0;
  1485. if (setjmp(bus_error_jmp) == 0) {
  1486. catch_memory_errors = 1;
  1487. sync();
  1488. p = (char *) adrs;
  1489. q = (char *) buf;
  1490. switch (size) {
  1491. case 2:
  1492. *(u16 *)p = *(u16 *)q;
  1493. break;
  1494. case 4:
  1495. *(u32 *)p = *(u32 *)q;
  1496. break;
  1497. case 8:
  1498. *(u64 *)p = *(u64 *)q;
  1499. break;
  1500. default:
  1501. for ( ; n < size; ++n) {
  1502. *p++ = *q++;
  1503. sync();
  1504. }
  1505. }
  1506. sync();
  1507. /* wait a little while to see if we get a machine check */
  1508. __delay(200);
  1509. n = size;
  1510. } else {
  1511. printf("*** Error writing address %x\n", adrs + n);
  1512. }
  1513. catch_memory_errors = 0;
  1514. return n;
  1515. }
  1516. static int fault_type;
  1517. static int fault_except;
  1518. static char *fault_chars[] = { "--", "**", "##" };
  1519. static int handle_fault(struct pt_regs *regs)
  1520. {
  1521. fault_except = TRAP(regs);
  1522. switch (TRAP(regs)) {
  1523. case 0x200:
  1524. fault_type = 0;
  1525. break;
  1526. case 0x300:
  1527. case 0x380:
  1528. fault_type = 1;
  1529. break;
  1530. default:
  1531. fault_type = 2;
  1532. }
  1533. longjmp(bus_error_jmp, 1);
  1534. return 0;
  1535. }
  1536. #define SWAP(a, b, t) ((t) = (a), (a) = (b), (b) = (t))
  1537. void
  1538. byterev(unsigned char *val, int size)
  1539. {
  1540. int t;
  1541. switch (size) {
  1542. case 2:
  1543. SWAP(val[0], val[1], t);
  1544. break;
  1545. case 4:
  1546. SWAP(val[0], val[3], t);
  1547. SWAP(val[1], val[2], t);
  1548. break;
  1549. case 8: /* is there really any use for this? */
  1550. SWAP(val[0], val[7], t);
  1551. SWAP(val[1], val[6], t);
  1552. SWAP(val[2], val[5], t);
  1553. SWAP(val[3], val[4], t);
  1554. break;
  1555. }
  1556. }
  1557. static int brev;
  1558. static int mnoread;
  1559. static char *memex_help_string =
  1560. "Memory examine command usage:\n"
  1561. "m [addr] [flags] examine/change memory\n"
  1562. " addr is optional. will start where left off.\n"
  1563. " flags may include chars from this set:\n"
  1564. " b modify by bytes (default)\n"
  1565. " w modify by words (2 byte)\n"
  1566. " l modify by longs (4 byte)\n"
  1567. " d modify by doubleword (8 byte)\n"
  1568. " r toggle reverse byte order mode\n"
  1569. " n do not read memory (for i/o spaces)\n"
  1570. " . ok to read (default)\n"
  1571. "NOTE: flags are saved as defaults\n"
  1572. "";
  1573. static char *memex_subcmd_help_string =
  1574. "Memory examine subcommands:\n"
  1575. " hexval write this val to current location\n"
  1576. " 'string' write chars from string to this location\n"
  1577. " ' increment address\n"
  1578. " ^ decrement address\n"
  1579. " / increment addr by 0x10. //=0x100, ///=0x1000, etc\n"
  1580. " \\ decrement addr by 0x10. \\\\=0x100, \\\\\\=0x1000, etc\n"
  1581. " ` clear no-read flag\n"
  1582. " ; stay at this addr\n"
  1583. " v change to byte mode\n"
  1584. " w change to word (2 byte) mode\n"
  1585. " l change to long (4 byte) mode\n"
  1586. " u change to doubleword (8 byte) mode\n"
  1587. " m addr change current addr\n"
  1588. " n toggle no-read flag\n"
  1589. " r toggle byte reverse flag\n"
  1590. " < count back up count bytes\n"
  1591. " > count skip forward count bytes\n"
  1592. " x exit this mode\n"
  1593. "";
  1594. void
  1595. memex(void)
  1596. {
  1597. int cmd, inc, i, nslash;
  1598. unsigned long n;
  1599. unsigned char val[16];
  1600. scanhex((void *)&adrs);
  1601. cmd = skipbl();
  1602. if (cmd == '?') {
  1603. printf(memex_help_string);
  1604. return;
  1605. } else {
  1606. termch = cmd;
  1607. }
  1608. last_cmd = "m\n";
  1609. while ((cmd = skipbl()) != '\n') {
  1610. switch( cmd ){
  1611. case 'b': size = 1; break;
  1612. case 'w': size = 2; break;
  1613. case 'l': size = 4; break;
  1614. case 'd': size = 8; break;
  1615. case 'r': brev = !brev; break;
  1616. case 'n': mnoread = 1; break;
  1617. case '.': mnoread = 0; break;
  1618. }
  1619. }
  1620. if( size <= 0 )
  1621. size = 1;
  1622. else if( size > 8 )
  1623. size = 8;
  1624. for(;;){
  1625. if (!mnoread)
  1626. n = mread(adrs, val, size);
  1627. printf(REG"%c", adrs, brev? 'r': ' ');
  1628. if (!mnoread) {
  1629. if (brev)
  1630. byterev(val, size);
  1631. putchar(' ');
  1632. for (i = 0; i < n; ++i)
  1633. printf("%.2x", val[i]);
  1634. for (; i < size; ++i)
  1635. printf("%s", fault_chars[fault_type]);
  1636. }
  1637. putchar(' ');
  1638. inc = size;
  1639. nslash = 0;
  1640. for(;;){
  1641. if( scanhex(&n) ){
  1642. for (i = 0; i < size; ++i)
  1643. val[i] = n >> (i * 8);
  1644. if (!brev)
  1645. byterev(val, size);
  1646. mwrite(adrs, val, size);
  1647. inc = size;
  1648. }
  1649. cmd = skipbl();
  1650. if (cmd == '\n')
  1651. break;
  1652. inc = 0;
  1653. switch (cmd) {
  1654. case '\'':
  1655. for(;;){
  1656. n = inchar();
  1657. if( n == '\\' )
  1658. n = bsesc();
  1659. else if( n == '\'' )
  1660. break;
  1661. for (i = 0; i < size; ++i)
  1662. val[i] = n >> (i * 8);
  1663. if (!brev)
  1664. byterev(val, size);
  1665. mwrite(adrs, val, size);
  1666. adrs += size;
  1667. }
  1668. adrs -= size;
  1669. inc = size;
  1670. break;
  1671. case ',':
  1672. adrs += size;
  1673. break;
  1674. case '.':
  1675. mnoread = 0;
  1676. break;
  1677. case ';':
  1678. break;
  1679. case 'x':
  1680. case EOF:
  1681. scannl();
  1682. return;
  1683. case 'b':
  1684. case 'v':
  1685. size = 1;
  1686. break;
  1687. case 'w':
  1688. size = 2;
  1689. break;
  1690. case 'l':
  1691. size = 4;
  1692. break;
  1693. case 'u':
  1694. size = 8;
  1695. break;
  1696. case '^':
  1697. adrs -= size;
  1698. break;
  1699. break;
  1700. case '/':
  1701. if (nslash > 0)
  1702. adrs -= 1 << nslash;
  1703. else
  1704. nslash = 0;
  1705. nslash += 4;
  1706. adrs += 1 << nslash;
  1707. break;
  1708. case '\\':
  1709. if (nslash < 0)
  1710. adrs += 1 << -nslash;
  1711. else
  1712. nslash = 0;
  1713. nslash -= 4;
  1714. adrs -= 1 << -nslash;
  1715. break;
  1716. case 'm':
  1717. scanhex((void *)&adrs);
  1718. break;
  1719. case 'n':
  1720. mnoread = 1;
  1721. break;
  1722. case 'r':
  1723. brev = !brev;
  1724. break;
  1725. case '<':
  1726. n = size;
  1727. scanhex(&n);
  1728. adrs -= n;
  1729. break;
  1730. case '>':
  1731. n = size;
  1732. scanhex(&n);
  1733. adrs += n;
  1734. break;
  1735. case '?':
  1736. printf(memex_subcmd_help_string);
  1737. break;
  1738. }
  1739. }
  1740. adrs += inc;
  1741. }
  1742. }
  1743. int
  1744. bsesc(void)
  1745. {
  1746. int c;
  1747. c = inchar();
  1748. switch( c ){
  1749. case 'n': c = '\n'; break;
  1750. case 'r': c = '\r'; break;
  1751. case 'b': c = '\b'; break;
  1752. case 't': c = '\t'; break;
  1753. }
  1754. return c;
  1755. }
  1756. #define isxdigit(c) (('0' <= (c) && (c) <= '9') \
  1757. || ('a' <= (c) && (c) <= 'f') \
  1758. || ('A' <= (c) && (c) <= 'F'))
  1759. void
  1760. dump(void)
  1761. {
  1762. int c;
  1763. c = inchar();
  1764. if ((isxdigit(c) && c != 'f' && c != 'd') || c == '\n')
  1765. termch = c;
  1766. scanhex((void *)&adrs);
  1767. if (termch != '\n')
  1768. termch = 0;
  1769. if (c == 'i') {
  1770. scanhex(&nidump);
  1771. if (nidump == 0)
  1772. nidump = 16;
  1773. else if (nidump > MAX_DUMP)
  1774. nidump = MAX_DUMP;
  1775. adrs += ppc_inst_dump(adrs, nidump, 1);
  1776. last_cmd = "di\n";
  1777. } else {
  1778. scanhex(&ndump);
  1779. if (ndump == 0)
  1780. ndump = 64;
  1781. else if (ndump > MAX_DUMP)
  1782. ndump = MAX_DUMP;
  1783. prdump(adrs, ndump);
  1784. adrs += ndump;
  1785. last_cmd = "d\n";
  1786. }
  1787. }
  1788. void
  1789. prdump(unsigned long adrs, long ndump)
  1790. {
  1791. long n, m, c, r, nr;
  1792. unsigned char temp[16];
  1793. for (n = ndump; n > 0;) {
  1794. printf(REG, adrs);
  1795. putchar(' ');
  1796. r = n < 16? n: 16;
  1797. nr = mread(adrs, temp, r);
  1798. adrs += nr;
  1799. for (m = 0; m < r; ++m) {
  1800. if ((m & (sizeof(long) - 1)) == 0 && m > 0)
  1801. putchar(' ');
  1802. if (m < nr)
  1803. printf("%.2x", temp[m]);
  1804. else
  1805. printf("%s", fault_chars[fault_type]);
  1806. }
  1807. for (; m < 16; ++m) {
  1808. if ((m & (sizeof(long) - 1)) == 0)
  1809. putchar(' ');
  1810. printf(" ");
  1811. }
  1812. printf(" |");
  1813. for (m = 0; m < r; ++m) {
  1814. if (m < nr) {
  1815. c = temp[m];
  1816. putchar(' ' <= c && c <= '~'? c: '.');
  1817. } else
  1818. putchar(' ');
  1819. }
  1820. n -= r;
  1821. for (; m < 16; ++m)
  1822. putchar(' ');
  1823. printf("|\n");
  1824. if (nr < r)
  1825. break;
  1826. }
  1827. }
  1828. int
  1829. ppc_inst_dump(unsigned long adr, long count, int praddr)
  1830. {
  1831. int nr, dotted;
  1832. unsigned long first_adr;
  1833. unsigned long inst, last_inst = 0;
  1834. unsigned char val[4];
  1835. dotted = 0;
  1836. for (first_adr = adr; count > 0; --count, adr += 4) {
  1837. nr = mread(adr, val, 4);
  1838. if (nr == 0) {
  1839. if (praddr) {
  1840. const char *x = fault_chars[fault_type];
  1841. printf(REG" %s%s%s%s\n", adr, x, x, x, x);
  1842. }
  1843. break;
  1844. }
  1845. inst = GETWORD(val);
  1846. if (adr > first_adr && inst == last_inst) {
  1847. if (!dotted) {
  1848. printf(" ...\n");
  1849. dotted = 1;
  1850. }
  1851. continue;
  1852. }
  1853. dotted = 0;
  1854. last_inst = inst;
  1855. if (praddr)
  1856. printf(REG" %.8x", adr, inst);
  1857. printf("\t");
  1858. print_insn_powerpc(inst, adr, 0); /* always returns 4 */
  1859. printf("\n");
  1860. }
  1861. return adr - first_adr;
  1862. }
  1863. void
  1864. print_address(unsigned long addr)
  1865. {
  1866. xmon_print_symbol(addr, "\t# ", "");
  1867. }
  1868. /*
  1869. * Memory operations - move, set, print differences
  1870. */
  1871. static unsigned long mdest; /* destination address */
  1872. static unsigned long msrc; /* source address */
  1873. static unsigned long mval; /* byte value to set memory to */
  1874. static unsigned long mcount; /* # bytes to affect */
  1875. static unsigned long mdiffs; /* max # differences to print */
  1876. void
  1877. memops(int cmd)
  1878. {
  1879. scanhex((void *)&mdest);
  1880. if( termch != '\n' )
  1881. termch = 0;
  1882. scanhex((void *)(cmd == 's'? &mval: &msrc));
  1883. if( termch != '\n' )
  1884. termch = 0;
  1885. scanhex((void *)&mcount);
  1886. switch( cmd ){
  1887. case 'm':
  1888. memmove((void *)mdest, (void *)msrc, mcount);
  1889. break;
  1890. case 's':
  1891. memset((void *)mdest, mval, mcount);
  1892. break;
  1893. case 'd':
  1894. if( termch != '\n' )
  1895. termch = 0;
  1896. scanhex((void *)&mdiffs);
  1897. memdiffs((unsigned char *)mdest, (unsigned char *)msrc, mcount, mdiffs);
  1898. break;
  1899. }
  1900. }
  1901. void
  1902. memdiffs(unsigned char *p1, unsigned char *p2, unsigned nb, unsigned maxpr)
  1903. {
  1904. unsigned n, prt;
  1905. prt = 0;
  1906. for( n = nb; n > 0; --n )
  1907. if( *p1++ != *p2++ )
  1908. if( ++prt <= maxpr )
  1909. printf("%.16x %.2x # %.16x %.2x\n", p1 - 1,
  1910. p1[-1], p2 - 1, p2[-1]);
  1911. if( prt > maxpr )
  1912. printf("Total of %d differences\n", prt);
  1913. }
  1914. static unsigned mend;
  1915. static unsigned mask;
  1916. void
  1917. memlocate(void)
  1918. {
  1919. unsigned a, n;
  1920. unsigned char val[4];
  1921. last_cmd = "ml";
  1922. scanhex((void *)&mdest);
  1923. if (termch != '\n') {
  1924. termch = 0;
  1925. scanhex((void *)&mend);
  1926. if (termch != '\n') {
  1927. termch = 0;
  1928. scanhex((void *)&mval);
  1929. mask = ~0;
  1930. if (termch != '\n') termch = 0;
  1931. scanhex((void *)&mask);
  1932. }
  1933. }
  1934. n = 0;
  1935. for (a = mdest; a < mend; a += 4) {
  1936. if (mread(a, val, 4) == 4
  1937. && ((GETWORD(val) ^ mval) & mask) == 0) {
  1938. printf("%.16x: %.16x\n", a, GETWORD(val));
  1939. if (++n >= 10)
  1940. break;
  1941. }
  1942. }
  1943. }
  1944. static unsigned long mskip = 0x1000;
  1945. static unsigned long mlim = 0xffffffff;
  1946. void
  1947. memzcan(void)
  1948. {
  1949. unsigned char v;
  1950. unsigned a;
  1951. int ok, ook;
  1952. scanhex(&mdest);
  1953. if (termch != '\n') termch = 0;
  1954. scanhex(&mskip);
  1955. if (termch != '\n') termch = 0;
  1956. scanhex(&mlim);
  1957. ook = 0;
  1958. for (a = mdest; a < mlim; a += mskip) {
  1959. ok = mread(a, &v, 1);
  1960. if (ok && !ook) {
  1961. printf("%.8x .. ", a);
  1962. } else if (!ok && ook)
  1963. printf("%.8x\n", a - mskip);
  1964. ook = ok;
  1965. if (a + mskip < a)
  1966. break;
  1967. }
  1968. if (ook)
  1969. printf("%.8x\n", a - mskip);
  1970. }
  1971. void proccall(void)
  1972. {
  1973. unsigned long args[8];
  1974. unsigned long ret;
  1975. int i;
  1976. typedef unsigned long (*callfunc_t)(unsigned long, unsigned long,
  1977. unsigned long, unsigned long, unsigned long,
  1978. unsigned long, unsigned long, unsigned long);
  1979. callfunc_t func;
  1980. if (!scanhex(&adrs))
  1981. return;
  1982. if (termch != '\n')
  1983. termch = 0;
  1984. for (i = 0; i < 8; ++i)
  1985. args[i] = 0;
  1986. for (i = 0; i < 8; ++i) {
  1987. if (!scanhex(&args[i]) || termch == '\n')
  1988. break;
  1989. termch = 0;
  1990. }
  1991. func = (callfunc_t) adrs;
  1992. ret = 0;
  1993. if (setjmp(bus_error_jmp) == 0) {
  1994. catch_memory_errors = 1;
  1995. sync();
  1996. ret = func(args[0], args[1], args[2], args[3],
  1997. args[4], args[5], args[6], args[7]);
  1998. sync();
  1999. printf("return value is %x\n", ret);
  2000. } else {
  2001. printf("*** %x exception occurred\n", fault_except);
  2002. }
  2003. catch_memory_errors = 0;
  2004. }
  2005. /* Input scanning routines */
  2006. int
  2007. skipbl(void)
  2008. {
  2009. int c;
  2010. if( termch != 0 ){
  2011. c = termch;
  2012. termch = 0;
  2013. } else
  2014. c = inchar();
  2015. while( c == ' ' || c == '\t' )
  2016. c = inchar();
  2017. return c;
  2018. }
  2019. #define N_PTREGS 44
  2020. static char *regnames[N_PTREGS] = {
  2021. "r0", "r1", "r2", "r3", "r4", "r5", "r6", "r7",
  2022. "r8", "r9", "r10", "r11", "r12", "r13", "r14", "r15",
  2023. "r16", "r17", "r18", "r19", "r20", "r21", "r22", "r23",
  2024. "r24", "r25", "r26", "r27", "r28", "r29", "r30", "r31",
  2025. "pc", "msr", "or3", "ctr", "lr", "xer", "ccr",
  2026. #ifdef CONFIG_PPC64
  2027. "softe",
  2028. #else
  2029. "mq",
  2030. #endif
  2031. "trap", "dar", "dsisr", "res"
  2032. };
  2033. int
  2034. scanhex(unsigned long *vp)
  2035. {
  2036. int c, d;
  2037. unsigned long v;
  2038. c = skipbl();
  2039. if (c == '%') {
  2040. /* parse register name */
  2041. char regname[8];
  2042. int i;
  2043. for (i = 0; i < sizeof(regname) - 1; ++i) {
  2044. c = inchar();
  2045. if (!isalnum(c)) {
  2046. termch = c;
  2047. break;
  2048. }
  2049. regname[i] = c;
  2050. }
  2051. regname[i] = 0;
  2052. for (i = 0; i < N_PTREGS; ++i) {
  2053. if (strcmp(regnames[i], regname) == 0) {
  2054. if (xmon_regs == NULL) {
  2055. printf("regs not available\n");
  2056. return 0;
  2057. }
  2058. *vp = ((unsigned long *)xmon_regs)[i];
  2059. return 1;
  2060. }
  2061. }
  2062. printf("invalid register name '%%%s'\n", regname);
  2063. return 0;
  2064. }
  2065. /* skip leading "0x" if any */
  2066. if (c == '0') {
  2067. c = inchar();
  2068. if (c == 'x') {
  2069. c = inchar();
  2070. } else {
  2071. d = hexdigit(c);
  2072. if (d == EOF) {
  2073. termch = c;
  2074. *vp = 0;
  2075. return 1;
  2076. }
  2077. }
  2078. } else if (c == '$') {
  2079. int i;
  2080. for (i=0; i<63; i++) {
  2081. c = inchar();
  2082. if (isspace(c)) {
  2083. termch = c;
  2084. break;
  2085. }
  2086. tmpstr[i] = c;
  2087. }
  2088. tmpstr[i++] = 0;
  2089. *vp = 0;
  2090. if (setjmp(bus_error_jmp) == 0) {
  2091. catch_memory_errors = 1;
  2092. sync();
  2093. *vp = kallsyms_lookup_name(tmpstr);
  2094. sync();
  2095. }
  2096. catch_memory_errors = 0;
  2097. if (!(*vp)) {
  2098. printf("unknown symbol '%s'\n", tmpstr);
  2099. return 0;
  2100. }
  2101. return 1;
  2102. }
  2103. d = hexdigit(c);
  2104. if (d == EOF) {
  2105. termch = c;
  2106. return 0;
  2107. }
  2108. v = 0;
  2109. do {
  2110. v = (v << 4) + d;
  2111. c = inchar();
  2112. d = hexdigit(c);
  2113. } while (d != EOF);
  2114. termch = c;
  2115. *vp = v;
  2116. return 1;
  2117. }
  2118. void
  2119. scannl(void)
  2120. {
  2121. int c;
  2122. c = termch;
  2123. termch = 0;
  2124. while( c != '\n' )
  2125. c = inchar();
  2126. }
  2127. int hexdigit(int c)
  2128. {
  2129. if( '0' <= c && c <= '9' )
  2130. return c - '0';
  2131. if( 'A' <= c && c <= 'F' )
  2132. return c - ('A' - 10);
  2133. if( 'a' <= c && c <= 'f' )
  2134. return c - ('a' - 10);
  2135. return EOF;
  2136. }
  2137. void
  2138. getstring(char *s, int size)
  2139. {
  2140. int c;
  2141. c = skipbl();
  2142. do {
  2143. if( size > 1 ){
  2144. *s++ = c;
  2145. --size;
  2146. }
  2147. c = inchar();
  2148. } while( c != ' ' && c != '\t' && c != '\n' );
  2149. termch = c;
  2150. *s = 0;
  2151. }
  2152. static char line[256];
  2153. static char *lineptr;
  2154. void
  2155. flush_input(void)
  2156. {
  2157. lineptr = NULL;
  2158. }
  2159. int
  2160. inchar(void)
  2161. {
  2162. if (lineptr == NULL || *lineptr == 0) {
  2163. if (xmon_gets(line, sizeof(line)) == NULL) {
  2164. lineptr = NULL;
  2165. return EOF;
  2166. }
  2167. lineptr = line;
  2168. }
  2169. return *lineptr++;
  2170. }
  2171. void
  2172. take_input(char *str)
  2173. {
  2174. lineptr = str;
  2175. }
  2176. static void
  2177. symbol_lookup(void)
  2178. {
  2179. int type = inchar();
  2180. unsigned long addr;
  2181. static char tmp[64];
  2182. switch (type) {
  2183. case 'a':
  2184. if (scanhex(&addr))
  2185. xmon_print_symbol(addr, ": ", "\n");
  2186. termch = 0;
  2187. break;
  2188. case 's':
  2189. getstring(tmp, 64);
  2190. if (setjmp(bus_error_jmp) == 0) {
  2191. catch_memory_errors = 1;
  2192. sync();
  2193. addr = kallsyms_lookup_name(tmp);
  2194. if (addr)
  2195. printf("%s: %lx\n", tmp, addr);
  2196. else
  2197. printf("Symbol '%s' not found.\n", tmp);
  2198. sync();
  2199. }
  2200. catch_memory_errors = 0;
  2201. termch = 0;
  2202. break;
  2203. }
  2204. }
  2205. /* Print an address in numeric and symbolic form (if possible) */
  2206. static void xmon_print_symbol(unsigned long address, const char *mid,
  2207. const char *after)
  2208. {
  2209. char *modname;
  2210. const char *name = NULL;
  2211. unsigned long offset, size;
  2212. printf(REG, address);
  2213. if (setjmp(bus_error_jmp) == 0) {
  2214. catch_memory_errors = 1;
  2215. sync();
  2216. name = kallsyms_lookup(address, &size, &offset, &modname,
  2217. tmpstr);
  2218. sync();
  2219. /* wait a little while to see if we get a machine check */
  2220. __delay(200);
  2221. }
  2222. catch_memory_errors = 0;
  2223. if (name) {
  2224. printf("%s%s+%#lx/%#lx", mid, name, offset, size);
  2225. if (modname)
  2226. printf(" [%s]", modname);
  2227. }
  2228. printf("%s", after);
  2229. }
  2230. #ifdef CONFIG_PPC64
  2231. static void dump_slb(void)
  2232. {
  2233. int i;
  2234. unsigned long tmp;
  2235. printf("SLB contents of cpu %x\n", smp_processor_id());
  2236. for (i = 0; i < SLB_NUM_ENTRIES; i++) {
  2237. asm volatile("slbmfee %0,%1" : "=r" (tmp) : "r" (i));
  2238. printf("%02d %016lx ", i, tmp);
  2239. asm volatile("slbmfev %0,%1" : "=r" (tmp) : "r" (i));
  2240. printf("%016lx\n", tmp);
  2241. }
  2242. }
  2243. static void dump_stab(void)
  2244. {
  2245. int i;
  2246. unsigned long *tmp = (unsigned long *)get_paca()->stab_addr;
  2247. printf("Segment table contents of cpu %x\n", smp_processor_id());
  2248. for (i = 0; i < PAGE_SIZE/16; i++) {
  2249. unsigned long a, b;
  2250. a = *tmp++;
  2251. b = *tmp++;
  2252. if (a || b) {
  2253. printf("%03d %016lx ", i, a);
  2254. printf("%016lx\n", b);
  2255. }
  2256. }
  2257. }
  2258. void dump_segments(void)
  2259. {
  2260. if (cpu_has_feature(CPU_FTR_SLB))
  2261. dump_slb();
  2262. else
  2263. dump_stab();
  2264. }
  2265. #endif
  2266. #ifdef CONFIG_PPC_STD_MMU_32
  2267. void dump_segments(void)
  2268. {
  2269. int i;
  2270. printf("sr0-15 =");
  2271. for (i = 0; i < 16; ++i)
  2272. printf(" %x", mfsrin(i));
  2273. printf("\n");
  2274. }
  2275. #endif
  2276. void xmon_init(int enable)
  2277. {
  2278. if (enable) {
  2279. __debugger = xmon;
  2280. __debugger_ipi = xmon_ipi;
  2281. __debugger_bpt = xmon_bpt;
  2282. __debugger_sstep = xmon_sstep;
  2283. __debugger_iabr_match = xmon_iabr_match;
  2284. __debugger_dabr_match = xmon_dabr_match;
  2285. __debugger_fault_handler = xmon_fault_handler;
  2286. } else {
  2287. __debugger = NULL;
  2288. __debugger_ipi = NULL;
  2289. __debugger_bpt = NULL;
  2290. __debugger_sstep = NULL;
  2291. __debugger_iabr_match = NULL;
  2292. __debugger_dabr_match = NULL;
  2293. __debugger_fault_handler = NULL;
  2294. }
  2295. xmon_map_scc();
  2296. }
  2297. #ifdef CONFIG_MAGIC_SYSRQ
  2298. static void sysrq_handle_xmon(int key, struct pt_regs *pt_regs,
  2299. struct tty_struct *tty)
  2300. {
  2301. /* ensure xmon is enabled */
  2302. xmon_init(1);
  2303. debugger(pt_regs);
  2304. }
  2305. static struct sysrq_key_op sysrq_xmon_op =
  2306. {
  2307. .handler = sysrq_handle_xmon,
  2308. .help_msg = "Xmon",
  2309. .action_msg = "Entering xmon",
  2310. };
  2311. static int __init setup_xmon_sysrq(void)
  2312. {
  2313. register_sysrq_key('x', &sysrq_xmon_op);
  2314. return 0;
  2315. }
  2316. __initcall(setup_xmon_sysrq);
  2317. #endif /* CONFIG_MAGIC_SYSRQ */