xmon.c 61 KB

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