xmon.c 54 KB

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