xmon.c 68 KB

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