xmon.c 53 KB

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