xmon.c 62 KB

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