xmon.c 54 KB

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