xmon.c 54 KB

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