xmon.c 68 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260
  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. #ifdef CONFIG_PPC64
  46. #include <asm/hvcall.h>
  47. #include <asm/paca.h>
  48. #endif
  49. #include "nonstdio.h"
  50. #include "dis-asm.h"
  51. #define scanhex xmon_scanhex
  52. #define skipbl xmon_skipbl
  53. #ifdef CONFIG_SMP
  54. static cpumask_t cpus_in_xmon = CPU_MASK_NONE;
  55. static unsigned long xmon_taken = 1;
  56. static int xmon_owner;
  57. static int xmon_gate;
  58. #else
  59. #define xmon_owner 0
  60. #endif /* CONFIG_SMP */
  61. static unsigned long in_xmon __read_mostly = 0;
  62. static unsigned long adrs;
  63. static int size = 1;
  64. #define MAX_DUMP (128 * 1024)
  65. static unsigned long ndump = 64;
  66. static unsigned long nidump = 16;
  67. static unsigned long ncsum = 4096;
  68. static int termch;
  69. static char tmpstr[128];
  70. static long bus_error_jmp[JMP_BUF_LEN];
  71. static int catch_memory_errors;
  72. static long *xmon_fault_jmp[NR_CPUS];
  73. /* Breakpoint stuff */
  74. struct bpt {
  75. unsigned long address;
  76. unsigned int instr[2];
  77. atomic_t ref_count;
  78. int enabled;
  79. unsigned long pad;
  80. };
  81. /* Bits in bpt.enabled */
  82. #define BP_IABR_TE 1 /* IABR translation enabled */
  83. #define BP_IABR 2
  84. #define BP_TRAP 8
  85. #define BP_DABR 0x10
  86. #define NBPTS 256
  87. static struct bpt bpts[NBPTS];
  88. static struct bpt dabr;
  89. static struct bpt *iabr;
  90. static unsigned bpinstr = 0x7fe00008; /* trap */
  91. #define BP_NUM(bp) ((bp) - bpts + 1)
  92. /* Prototypes */
  93. static int cmds(struct pt_regs *);
  94. static int mread(unsigned long, void *, int);
  95. static int mwrite(unsigned long, void *, int);
  96. static int handle_fault(struct pt_regs *);
  97. static void byterev(unsigned char *, int);
  98. static void memex(void);
  99. static int bsesc(void);
  100. static void dump(void);
  101. static void prdump(unsigned long, long);
  102. static int ppc_inst_dump(unsigned long, long, int);
  103. static void dump_log_buf(void);
  104. static void backtrace(struct pt_regs *);
  105. static void excprint(struct pt_regs *);
  106. static void prregs(struct pt_regs *);
  107. static void memops(int);
  108. static void memlocate(void);
  109. static void memzcan(void);
  110. static void memdiffs(unsigned char *, unsigned char *, unsigned, unsigned);
  111. int skipbl(void);
  112. int scanhex(unsigned long *valp);
  113. static void scannl(void);
  114. static int hexdigit(int);
  115. void getstring(char *, int);
  116. static void flush_input(void);
  117. static int inchar(void);
  118. static void take_input(char *);
  119. static unsigned long read_spr(int);
  120. static void write_spr(int, unsigned long);
  121. static void super_regs(void);
  122. static void remove_bpts(void);
  123. static void insert_bpts(void);
  124. static void remove_cpu_bpts(void);
  125. static void insert_cpu_bpts(void);
  126. static struct bpt *at_breakpoint(unsigned long pc);
  127. static struct bpt *in_breakpoint_table(unsigned long pc, unsigned long *offp);
  128. static int do_step(struct pt_regs *);
  129. static void bpt_cmds(void);
  130. static void cacheflush(void);
  131. static int cpu_cmd(void);
  132. static void csum(void);
  133. static void bootcmds(void);
  134. static void proccall(void);
  135. void dump_segments(void);
  136. static void symbol_lookup(void);
  137. static void xmon_show_stack(unsigned long sp, unsigned long lr,
  138. unsigned long pc);
  139. static void xmon_print_symbol(unsigned long address, const char *mid,
  140. const char *after);
  141. static const char *getvecname(unsigned long vec);
  142. static int do_spu_cmd(void);
  143. #ifdef CONFIG_44x
  144. static void dump_tlb_44x(void);
  145. #endif
  146. #ifdef CONFIG_PPC_BOOK3E
  147. static void dump_tlb_book3e(void);
  148. #endif
  149. static int xmon_no_auto_backtrace;
  150. extern void xmon_enter(void);
  151. extern void xmon_leave(void);
  152. #ifdef CONFIG_PPC64
  153. #define REG "%.16lx"
  154. #define REGS_PER_LINE 4
  155. #define LAST_VOLATILE 13
  156. #else
  157. #define REG "%.8lx"
  158. #define REGS_PER_LINE 8
  159. #define LAST_VOLATILE 12
  160. #endif
  161. #define GETWORD(v) (((v)[0] << 24) + ((v)[1] << 16) + ((v)[2] << 8) + (v)[3])
  162. #define isxdigit(c) (('0' <= (c) && (c) <= '9') \
  163. || ('a' <= (c) && (c) <= 'f') \
  164. || ('A' <= (c) && (c) <= 'F'))
  165. #define isalnum(c) (('0' <= (c) && (c) <= '9') \
  166. || ('a' <= (c) && (c) <= 'z') \
  167. || ('A' <= (c) && (c) <= 'Z'))
  168. #define isspace(c) (c == ' ' || c == '\t' || c == 10 || c == 13 || c == 0)
  169. static char *help_string = "\
  170. Commands:\n\
  171. b show breakpoints\n\
  172. bd set data breakpoint\n\
  173. bi set instruction breakpoint\n\
  174. bc clear breakpoint\n"
  175. #ifdef CONFIG_SMP
  176. "\
  177. c print cpus stopped in xmon\n\
  178. c# try to switch to cpu number h (in hex)\n"
  179. #endif
  180. "\
  181. C checksum\n\
  182. d dump bytes\n\
  183. di dump instructions\n\
  184. df dump float values\n\
  185. dd dump double values\n\
  186. dl dump the kernel log buffer\n"
  187. #ifdef CONFIG_PPC64
  188. "\
  189. dp[#] dump paca for current cpu, or cpu #\n\
  190. dpa dump paca for all possible cpus\n"
  191. #endif
  192. "\
  193. dr dump stream of raw bytes\n\
  194. e print exception information\n\
  195. f flush cache\n\
  196. la lookup symbol+offset of specified address\n\
  197. ls lookup address of specified symbol\n\
  198. m examine/change memory\n\
  199. mm move a block of memory\n\
  200. ms set a block of memory\n\
  201. md compare two blocks of memory\n\
  202. ml locate a block of memory\n\
  203. mz zero a block of memory\n\
  204. mi show information about memory allocation\n\
  205. p call a procedure\n\
  206. r print registers\n\
  207. s single step\n"
  208. #ifdef CONFIG_SPU_BASE
  209. " ss stop execution on all spus\n\
  210. sr restore execution on stopped spus\n\
  211. sf # dump spu fields for spu # (in hex)\n\
  212. sd # dump spu local store for spu # (in hex)\n\
  213. sdi # disassemble spu local store for spu # (in hex)\n"
  214. #endif
  215. " S print special registers\n\
  216. t print backtrace\n\
  217. x exit monitor and recover\n\
  218. X exit monitor and dont recover\n"
  219. #if defined(CONFIG_PPC64) && !defined(CONFIG_PPC_BOOK3E)
  220. " u dump segment table or SLB\n"
  221. #elif defined(CONFIG_PPC_STD_MMU_32)
  222. " u dump segment registers\n"
  223. #elif defined(CONFIG_44x) || defined(CONFIG_PPC_BOOK3E)
  224. " u dump TLB\n"
  225. #endif
  226. " ? help\n"
  227. " zr reboot\n\
  228. zh halt\n"
  229. ;
  230. static struct pt_regs *xmon_regs;
  231. static inline void sync(void)
  232. {
  233. asm volatile("sync; isync");
  234. }
  235. static inline void store_inst(void *p)
  236. {
  237. asm volatile ("dcbst 0,%0; sync; icbi 0,%0; isync" : : "r" (p));
  238. }
  239. static inline void cflush(void *p)
  240. {
  241. asm volatile ("dcbf 0,%0; icbi 0,%0" : : "r" (p));
  242. }
  243. static inline void cinval(void *p)
  244. {
  245. asm volatile ("dcbi 0,%0; icbi 0,%0" : : "r" (p));
  246. }
  247. /*
  248. * Disable surveillance (the service processor watchdog function)
  249. * while we are in xmon.
  250. * XXX we should re-enable it when we leave. :)
  251. */
  252. #define SURVEILLANCE_TOKEN 9000
  253. static inline void disable_surveillance(void)
  254. {
  255. #ifdef CONFIG_PPC_PSERIES
  256. /* Since this can't be a module, args should end up below 4GB. */
  257. static struct rtas_args args;
  258. /*
  259. * At this point we have got all the cpus we can into
  260. * xmon, so there is hopefully no other cpu calling RTAS
  261. * at the moment, even though we don't take rtas.lock.
  262. * If we did try to take rtas.lock there would be a
  263. * real possibility of deadlock.
  264. */
  265. args.token = rtas_token("set-indicator");
  266. if (args.token == RTAS_UNKNOWN_SERVICE)
  267. return;
  268. args.nargs = 3;
  269. args.nret = 1;
  270. args.rets = &args.args[3];
  271. args.args[0] = SURVEILLANCE_TOKEN;
  272. args.args[1] = 0;
  273. args.args[2] = 0;
  274. enter_rtas(__pa(&args));
  275. #endif /* CONFIG_PPC_PSERIES */
  276. }
  277. #ifdef CONFIG_SMP
  278. static int xmon_speaker;
  279. static void get_output_lock(void)
  280. {
  281. int me = smp_processor_id() + 0x100;
  282. int last_speaker = 0, prev;
  283. long timeout;
  284. if (xmon_speaker == me)
  285. return;
  286. for (;;) {
  287. if (xmon_speaker == 0) {
  288. last_speaker = cmpxchg(&xmon_speaker, 0, me);
  289. if (last_speaker == 0)
  290. return;
  291. }
  292. timeout = 10000000;
  293. while (xmon_speaker == last_speaker) {
  294. if (--timeout > 0)
  295. continue;
  296. /* hostile takeover */
  297. prev = cmpxchg(&xmon_speaker, last_speaker, me);
  298. if (prev == last_speaker)
  299. return;
  300. break;
  301. }
  302. }
  303. }
  304. static void release_output_lock(void)
  305. {
  306. xmon_speaker = 0;
  307. }
  308. int cpus_are_in_xmon(void)
  309. {
  310. return !cpumask_empty(&cpus_in_xmon);
  311. }
  312. #endif
  313. static inline int unrecoverable_excp(struct pt_regs *regs)
  314. {
  315. #if defined(CONFIG_4xx) || defined(CONFIG_PPC_BOOK3E)
  316. /* We have no MSR_RI bit on 4xx or Book3e, so we simply return false */
  317. return 0;
  318. #else
  319. return ((regs->msr & MSR_RI) == 0);
  320. #endif
  321. }
  322. static int xmon_core(struct pt_regs *regs, int fromipi)
  323. {
  324. int cmd = 0;
  325. struct bpt *bp;
  326. long recurse_jmp[JMP_BUF_LEN];
  327. unsigned long offset;
  328. unsigned long flags;
  329. #ifdef CONFIG_SMP
  330. int cpu;
  331. int secondary;
  332. unsigned long timeout;
  333. #endif
  334. local_irq_save(flags);
  335. bp = in_breakpoint_table(regs->nip, &offset);
  336. if (bp != NULL) {
  337. regs->nip = bp->address + offset;
  338. atomic_dec(&bp->ref_count);
  339. }
  340. remove_cpu_bpts();
  341. #ifdef CONFIG_SMP
  342. cpu = smp_processor_id();
  343. if (cpumask_test_cpu(cpu, &cpus_in_xmon)) {
  344. get_output_lock();
  345. excprint(regs);
  346. printf("cpu 0x%x: Exception %lx %s in xmon, "
  347. "returning to main loop\n",
  348. cpu, regs->trap, getvecname(TRAP(regs)));
  349. release_output_lock();
  350. longjmp(xmon_fault_jmp[cpu], 1);
  351. }
  352. if (setjmp(recurse_jmp) != 0) {
  353. if (!in_xmon || !xmon_gate) {
  354. get_output_lock();
  355. printf("xmon: WARNING: bad recursive fault "
  356. "on cpu 0x%x\n", cpu);
  357. release_output_lock();
  358. goto waiting;
  359. }
  360. secondary = !(xmon_taken && cpu == xmon_owner);
  361. goto cmdloop;
  362. }
  363. xmon_fault_jmp[cpu] = recurse_jmp;
  364. cpumask_set_cpu(cpu, &cpus_in_xmon);
  365. bp = NULL;
  366. if ((regs->msr & (MSR_IR|MSR_PR|MSR_64BIT)) == (MSR_IR|MSR_64BIT))
  367. bp = at_breakpoint(regs->nip);
  368. if (bp || unrecoverable_excp(regs))
  369. fromipi = 0;
  370. if (!fromipi) {
  371. get_output_lock();
  372. excprint(regs);
  373. if (bp) {
  374. printf("cpu 0x%x stopped at breakpoint 0x%x (",
  375. cpu, BP_NUM(bp));
  376. xmon_print_symbol(regs->nip, " ", ")\n");
  377. }
  378. if (unrecoverable_excp(regs))
  379. printf("WARNING: exception is not recoverable, "
  380. "can't continue\n");
  381. release_output_lock();
  382. }
  383. waiting:
  384. secondary = 1;
  385. while (secondary && !xmon_gate) {
  386. if (in_xmon == 0) {
  387. if (fromipi)
  388. goto leave;
  389. secondary = test_and_set_bit(0, &in_xmon);
  390. }
  391. barrier();
  392. }
  393. if (!secondary && !xmon_gate) {
  394. /* we are the first cpu to come in */
  395. /* interrupt other cpu(s) */
  396. int ncpus = num_online_cpus();
  397. xmon_owner = cpu;
  398. mb();
  399. if (ncpus > 1) {
  400. smp_send_debugger_break();
  401. /* wait for other cpus to come in */
  402. for (timeout = 100000000; timeout != 0; --timeout) {
  403. if (cpumask_weight(&cpus_in_xmon) >= ncpus)
  404. break;
  405. barrier();
  406. }
  407. }
  408. remove_bpts();
  409. disable_surveillance();
  410. /* for breakpoint or single step, print the current instr. */
  411. if (bp || TRAP(regs) == 0xd00)
  412. ppc_inst_dump(regs->nip, 1, 0);
  413. printf("enter ? for help\n");
  414. mb();
  415. xmon_gate = 1;
  416. barrier();
  417. }
  418. cmdloop:
  419. while (in_xmon) {
  420. if (secondary) {
  421. if (cpu == xmon_owner) {
  422. if (!test_and_set_bit(0, &xmon_taken)) {
  423. secondary = 0;
  424. continue;
  425. }
  426. /* missed it */
  427. while (cpu == xmon_owner)
  428. barrier();
  429. }
  430. barrier();
  431. } else {
  432. cmd = cmds(regs);
  433. if (cmd != 0) {
  434. /* exiting xmon */
  435. insert_bpts();
  436. xmon_gate = 0;
  437. wmb();
  438. in_xmon = 0;
  439. break;
  440. }
  441. /* have switched to some other cpu */
  442. secondary = 1;
  443. }
  444. }
  445. leave:
  446. cpumask_clear_cpu(cpu, &cpus_in_xmon);
  447. xmon_fault_jmp[cpu] = NULL;
  448. #else
  449. /* UP is simple... */
  450. if (in_xmon) {
  451. printf("Exception %lx %s in xmon, returning to main loop\n",
  452. regs->trap, getvecname(TRAP(regs)));
  453. longjmp(xmon_fault_jmp[0], 1);
  454. }
  455. if (setjmp(recurse_jmp) == 0) {
  456. xmon_fault_jmp[0] = recurse_jmp;
  457. in_xmon = 1;
  458. excprint(regs);
  459. bp = at_breakpoint(regs->nip);
  460. if (bp) {
  461. printf("Stopped at breakpoint %x (", BP_NUM(bp));
  462. xmon_print_symbol(regs->nip, " ", ")\n");
  463. }
  464. if (unrecoverable_excp(regs))
  465. printf("WARNING: exception is not recoverable, "
  466. "can't continue\n");
  467. remove_bpts();
  468. disable_surveillance();
  469. /* for breakpoint or single step, print the current instr. */
  470. if (bp || TRAP(regs) == 0xd00)
  471. ppc_inst_dump(regs->nip, 1, 0);
  472. printf("enter ? for help\n");
  473. }
  474. cmd = cmds(regs);
  475. insert_bpts();
  476. in_xmon = 0;
  477. #endif
  478. #ifdef CONFIG_BOOKE
  479. if (regs->msr & MSR_DE) {
  480. bp = at_breakpoint(regs->nip);
  481. if (bp != NULL) {
  482. regs->nip = (unsigned long) &bp->instr[0];
  483. atomic_inc(&bp->ref_count);
  484. }
  485. }
  486. #else
  487. if ((regs->msr & (MSR_IR|MSR_PR|MSR_64BIT)) == (MSR_IR|MSR_64BIT)) {
  488. bp = at_breakpoint(regs->nip);
  489. if (bp != NULL) {
  490. int stepped = emulate_step(regs, bp->instr[0]);
  491. if (stepped == 0) {
  492. regs->nip = (unsigned long) &bp->instr[0];
  493. atomic_inc(&bp->ref_count);
  494. } else if (stepped < 0) {
  495. printf("Couldn't single-step %s instruction\n",
  496. (IS_RFID(bp->instr[0])? "rfid": "mtmsrd"));
  497. }
  498. }
  499. }
  500. #endif
  501. insert_cpu_bpts();
  502. local_irq_restore(flags);
  503. return cmd != 'X' && cmd != EOF;
  504. }
  505. int xmon(struct pt_regs *excp)
  506. {
  507. struct pt_regs regs;
  508. if (excp == NULL) {
  509. ppc_save_regs(&regs);
  510. excp = &regs;
  511. }
  512. return xmon_core(excp, 0);
  513. }
  514. EXPORT_SYMBOL(xmon);
  515. irqreturn_t xmon_irq(int irq, void *d)
  516. {
  517. unsigned long flags;
  518. local_irq_save(flags);
  519. printf("Keyboard interrupt\n");
  520. xmon(get_irq_regs());
  521. local_irq_restore(flags);
  522. return IRQ_HANDLED;
  523. }
  524. static int xmon_bpt(struct pt_regs *regs)
  525. {
  526. struct bpt *bp;
  527. unsigned long offset;
  528. if ((regs->msr & (MSR_IR|MSR_PR|MSR_64BIT)) != (MSR_IR|MSR_64BIT))
  529. return 0;
  530. /* Are we at the trap at bp->instr[1] for some bp? */
  531. bp = in_breakpoint_table(regs->nip, &offset);
  532. if (bp != NULL && offset == 4) {
  533. regs->nip = bp->address + 4;
  534. atomic_dec(&bp->ref_count);
  535. return 1;
  536. }
  537. /* Are we at a breakpoint? */
  538. bp = at_breakpoint(regs->nip);
  539. if (!bp)
  540. return 0;
  541. xmon_core(regs, 0);
  542. return 1;
  543. }
  544. static int xmon_sstep(struct pt_regs *regs)
  545. {
  546. if (user_mode(regs))
  547. return 0;
  548. xmon_core(regs, 0);
  549. return 1;
  550. }
  551. static int xmon_dabr_match(struct pt_regs *regs)
  552. {
  553. if ((regs->msr & (MSR_IR|MSR_PR|MSR_64BIT)) != (MSR_IR|MSR_64BIT))
  554. return 0;
  555. if (dabr.enabled == 0)
  556. return 0;
  557. xmon_core(regs, 0);
  558. return 1;
  559. }
  560. static int xmon_iabr_match(struct pt_regs *regs)
  561. {
  562. if ((regs->msr & (MSR_IR|MSR_PR|MSR_64BIT)) != (MSR_IR|MSR_64BIT))
  563. return 0;
  564. if (iabr == NULL)
  565. return 0;
  566. xmon_core(regs, 0);
  567. return 1;
  568. }
  569. static int xmon_ipi(struct pt_regs *regs)
  570. {
  571. #ifdef CONFIG_SMP
  572. if (in_xmon && !cpumask_test_cpu(smp_processor_id(), &cpus_in_xmon))
  573. xmon_core(regs, 1);
  574. #endif
  575. return 0;
  576. }
  577. static int xmon_fault_handler(struct pt_regs *regs)
  578. {
  579. struct bpt *bp;
  580. unsigned long offset;
  581. if (in_xmon && catch_memory_errors)
  582. handle_fault(regs); /* doesn't return */
  583. if ((regs->msr & (MSR_IR|MSR_PR|MSR_64BIT)) == (MSR_IR|MSR_64BIT)) {
  584. bp = in_breakpoint_table(regs->nip, &offset);
  585. if (bp != NULL) {
  586. regs->nip = bp->address + offset;
  587. atomic_dec(&bp->ref_count);
  588. }
  589. }
  590. return 0;
  591. }
  592. static struct bpt *at_breakpoint(unsigned long pc)
  593. {
  594. int i;
  595. struct bpt *bp;
  596. bp = bpts;
  597. for (i = 0; i < NBPTS; ++i, ++bp)
  598. if (bp->enabled && pc == bp->address)
  599. return bp;
  600. return NULL;
  601. }
  602. static struct bpt *in_breakpoint_table(unsigned long nip, unsigned long *offp)
  603. {
  604. unsigned long off;
  605. off = nip - (unsigned long) bpts;
  606. if (off >= sizeof(bpts))
  607. return NULL;
  608. off %= sizeof(struct bpt);
  609. if (off != offsetof(struct bpt, instr[0])
  610. && off != offsetof(struct bpt, instr[1]))
  611. return NULL;
  612. *offp = off - offsetof(struct bpt, instr[0]);
  613. return (struct bpt *) (nip - off);
  614. }
  615. static struct bpt *new_breakpoint(unsigned long a)
  616. {
  617. struct bpt *bp;
  618. a &= ~3UL;
  619. bp = at_breakpoint(a);
  620. if (bp)
  621. return bp;
  622. for (bp = bpts; bp < &bpts[NBPTS]; ++bp) {
  623. if (!bp->enabled && atomic_read(&bp->ref_count) == 0) {
  624. bp->address = a;
  625. bp->instr[1] = bpinstr;
  626. store_inst(&bp->instr[1]);
  627. return bp;
  628. }
  629. }
  630. printf("Sorry, no free breakpoints. Please clear one first.\n");
  631. return NULL;
  632. }
  633. static void insert_bpts(void)
  634. {
  635. int i;
  636. struct bpt *bp;
  637. bp = bpts;
  638. for (i = 0; i < NBPTS; ++i, ++bp) {
  639. if ((bp->enabled & (BP_TRAP|BP_IABR)) == 0)
  640. continue;
  641. if (mread(bp->address, &bp->instr[0], 4) != 4) {
  642. printf("Couldn't read instruction at %lx, "
  643. "disabling breakpoint there\n", bp->address);
  644. bp->enabled = 0;
  645. continue;
  646. }
  647. if (IS_MTMSRD(bp->instr[0]) || IS_RFID(bp->instr[0])) {
  648. printf("Breakpoint at %lx is on an mtmsrd or rfid "
  649. "instruction, disabling it\n", bp->address);
  650. bp->enabled = 0;
  651. continue;
  652. }
  653. store_inst(&bp->instr[0]);
  654. if (bp->enabled & BP_IABR)
  655. continue;
  656. if (mwrite(bp->address, &bpinstr, 4) != 4) {
  657. printf("Couldn't write instruction at %lx, "
  658. "disabling breakpoint there\n", bp->address);
  659. bp->enabled &= ~BP_TRAP;
  660. continue;
  661. }
  662. store_inst((void *)bp->address);
  663. }
  664. }
  665. static void insert_cpu_bpts(void)
  666. {
  667. if (dabr.enabled)
  668. set_dabr(dabr.address | (dabr.enabled & 7), DABRX_ALL);
  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. set_dabr(0, 0);
  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 &= ~7;
  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. static int xmon_depth_to_print = 64;
  1175. #define LRSAVE_OFFSET (STACK_FRAME_LR_SAVE * sizeof(unsigned long))
  1176. #define MARKER_OFFSET (STACK_FRAME_MARKER * sizeof(unsigned long))
  1177. #ifdef __powerpc64__
  1178. #define REGS_OFFSET 0x70
  1179. #else
  1180. #define REGS_OFFSET 16
  1181. #endif
  1182. static void xmon_show_stack(unsigned long sp, unsigned long lr,
  1183. unsigned long pc)
  1184. {
  1185. unsigned long ip;
  1186. unsigned long newsp;
  1187. unsigned long marker;
  1188. int count = 0;
  1189. struct pt_regs regs;
  1190. do {
  1191. if (sp < PAGE_OFFSET) {
  1192. if (sp != 0)
  1193. printf("SP (%lx) is in userspace\n", sp);
  1194. break;
  1195. }
  1196. if (!mread(sp + LRSAVE_OFFSET, &ip, sizeof(unsigned long))
  1197. || !mread(sp, &newsp, sizeof(unsigned long))) {
  1198. printf("Couldn't read stack frame at %lx\n", sp);
  1199. break;
  1200. }
  1201. /*
  1202. * For the first stack frame, try to work out if
  1203. * LR and/or the saved LR value in the bottommost
  1204. * stack frame are valid.
  1205. */
  1206. if ((pc | lr) != 0) {
  1207. unsigned long fnstart, fnend;
  1208. unsigned long nextip;
  1209. int printip = 1;
  1210. get_function_bounds(pc, &fnstart, &fnend);
  1211. nextip = 0;
  1212. if (newsp > sp)
  1213. mread(newsp + LRSAVE_OFFSET, &nextip,
  1214. sizeof(unsigned long));
  1215. if (lr == ip) {
  1216. if (lr < PAGE_OFFSET
  1217. || (fnstart <= lr && lr < fnend))
  1218. printip = 0;
  1219. } else if (lr == nextip) {
  1220. printip = 0;
  1221. } else if (lr >= PAGE_OFFSET
  1222. && !(fnstart <= lr && lr < fnend)) {
  1223. printf("[link register ] ");
  1224. xmon_print_symbol(lr, " ", "\n");
  1225. }
  1226. if (printip) {
  1227. printf("["REG"] ", sp);
  1228. xmon_print_symbol(ip, " ", " (unreliable)\n");
  1229. }
  1230. pc = lr = 0;
  1231. } else {
  1232. printf("["REG"] ", sp);
  1233. xmon_print_symbol(ip, " ", "\n");
  1234. }
  1235. /* Look for "regshere" marker to see if this is
  1236. an exception frame. */
  1237. if (mread(sp + MARKER_OFFSET, &marker, sizeof(unsigned long))
  1238. && marker == STACK_FRAME_REGS_MARKER) {
  1239. if (mread(sp + REGS_OFFSET, &regs, sizeof(regs))
  1240. != sizeof(regs)) {
  1241. printf("Couldn't read registers at %lx\n",
  1242. sp + REGS_OFFSET);
  1243. break;
  1244. }
  1245. printf("--- Exception: %lx %s at ", regs.trap,
  1246. getvecname(TRAP(&regs)));
  1247. pc = regs.nip;
  1248. lr = regs.link;
  1249. xmon_print_symbol(pc, " ", "\n");
  1250. }
  1251. if (newsp == 0)
  1252. break;
  1253. sp = newsp;
  1254. } while (count++ < xmon_depth_to_print);
  1255. }
  1256. static void backtrace(struct pt_regs *excp)
  1257. {
  1258. unsigned long sp;
  1259. if (scanhex(&sp))
  1260. xmon_show_stack(sp, 0, 0);
  1261. else
  1262. xmon_show_stack(excp->gpr[1], excp->link, excp->nip);
  1263. scannl();
  1264. }
  1265. static void print_bug_trap(struct pt_regs *regs)
  1266. {
  1267. #ifdef CONFIG_BUG
  1268. const struct bug_entry *bug;
  1269. unsigned long addr;
  1270. if (regs->msr & MSR_PR)
  1271. return; /* not in kernel */
  1272. addr = regs->nip; /* address of trap instruction */
  1273. if (addr < PAGE_OFFSET)
  1274. return;
  1275. bug = find_bug(regs->nip);
  1276. if (bug == NULL)
  1277. return;
  1278. if (is_warning_bug(bug))
  1279. return;
  1280. #ifdef CONFIG_DEBUG_BUGVERBOSE
  1281. printf("kernel BUG at %s:%u!\n",
  1282. bug->file, bug->line);
  1283. #else
  1284. printf("kernel BUG at %p!\n", (void *)bug->bug_addr);
  1285. #endif
  1286. #endif /* CONFIG_BUG */
  1287. }
  1288. static void excprint(struct pt_regs *fp)
  1289. {
  1290. unsigned long trap;
  1291. #ifdef CONFIG_SMP
  1292. printf("cpu 0x%x: ", smp_processor_id());
  1293. #endif /* CONFIG_SMP */
  1294. trap = TRAP(fp);
  1295. printf("Vector: %lx %s at [%lx]\n", fp->trap, getvecname(trap), fp);
  1296. printf(" pc: ");
  1297. xmon_print_symbol(fp->nip, ": ", "\n");
  1298. printf(" lr: ", fp->link);
  1299. xmon_print_symbol(fp->link, ": ", "\n");
  1300. printf(" sp: %lx\n", fp->gpr[1]);
  1301. printf(" msr: %lx\n", fp->msr);
  1302. if (trap == 0x300 || trap == 0x380 || trap == 0x600) {
  1303. printf(" dar: %lx\n", fp->dar);
  1304. if (trap != 0x380)
  1305. printf(" dsisr: %lx\n", fp->dsisr);
  1306. }
  1307. printf(" current = 0x%lx\n", current);
  1308. #ifdef CONFIG_PPC64
  1309. printf(" paca = 0x%lx\t softe: %d\t irq_happened: 0x%02x\n",
  1310. local_paca, local_paca->soft_enabled, local_paca->irq_happened);
  1311. #endif
  1312. if (current) {
  1313. printf(" pid = %ld, comm = %s\n",
  1314. current->pid, current->comm);
  1315. }
  1316. if (trap == 0x700)
  1317. print_bug_trap(fp);
  1318. }
  1319. static void prregs(struct pt_regs *fp)
  1320. {
  1321. int n, trap;
  1322. unsigned long base;
  1323. struct pt_regs regs;
  1324. if (scanhex(&base)) {
  1325. if (setjmp(bus_error_jmp) == 0) {
  1326. catch_memory_errors = 1;
  1327. sync();
  1328. regs = *(struct pt_regs *)base;
  1329. sync();
  1330. __delay(200);
  1331. } else {
  1332. catch_memory_errors = 0;
  1333. printf("*** Error reading registers from "REG"\n",
  1334. base);
  1335. return;
  1336. }
  1337. catch_memory_errors = 0;
  1338. fp = &regs;
  1339. }
  1340. #ifdef CONFIG_PPC64
  1341. if (FULL_REGS(fp)) {
  1342. for (n = 0; n < 16; ++n)
  1343. printf("R%.2ld = "REG" R%.2ld = "REG"\n",
  1344. n, fp->gpr[n], n+16, fp->gpr[n+16]);
  1345. } else {
  1346. for (n = 0; n < 7; ++n)
  1347. printf("R%.2ld = "REG" R%.2ld = "REG"\n",
  1348. n, fp->gpr[n], n+7, fp->gpr[n+7]);
  1349. }
  1350. #else
  1351. for (n = 0; n < 32; ++n) {
  1352. printf("R%.2d = %.8x%s", n, fp->gpr[n],
  1353. (n & 3) == 3? "\n": " ");
  1354. if (n == 12 && !FULL_REGS(fp)) {
  1355. printf("\n");
  1356. break;
  1357. }
  1358. }
  1359. #endif
  1360. printf("pc = ");
  1361. xmon_print_symbol(fp->nip, " ", "\n");
  1362. if (TRAP(fp) != 0xc00 && cpu_has_feature(CPU_FTR_CFAR)) {
  1363. printf("cfar= ");
  1364. xmon_print_symbol(fp->orig_gpr3, " ", "\n");
  1365. }
  1366. printf("lr = ");
  1367. xmon_print_symbol(fp->link, " ", "\n");
  1368. printf("msr = "REG" cr = %.8lx\n", fp->msr, fp->ccr);
  1369. printf("ctr = "REG" xer = "REG" trap = %4lx\n",
  1370. fp->ctr, fp->xer, fp->trap);
  1371. trap = TRAP(fp);
  1372. if (trap == 0x300 || trap == 0x380 || trap == 0x600)
  1373. printf("dar = "REG" dsisr = %.8lx\n", fp->dar, fp->dsisr);
  1374. }
  1375. static void cacheflush(void)
  1376. {
  1377. int cmd;
  1378. unsigned long nflush;
  1379. cmd = inchar();
  1380. if (cmd != 'i')
  1381. termch = cmd;
  1382. scanhex((void *)&adrs);
  1383. if (termch != '\n')
  1384. termch = 0;
  1385. nflush = 1;
  1386. scanhex(&nflush);
  1387. nflush = (nflush + L1_CACHE_BYTES - 1) / L1_CACHE_BYTES;
  1388. if (setjmp(bus_error_jmp) == 0) {
  1389. catch_memory_errors = 1;
  1390. sync();
  1391. if (cmd != 'i') {
  1392. for (; nflush > 0; --nflush, adrs += L1_CACHE_BYTES)
  1393. cflush((void *) adrs);
  1394. } else {
  1395. for (; nflush > 0; --nflush, adrs += L1_CACHE_BYTES)
  1396. cinval((void *) adrs);
  1397. }
  1398. sync();
  1399. /* wait a little while to see if we get a machine check */
  1400. __delay(200);
  1401. }
  1402. catch_memory_errors = 0;
  1403. }
  1404. static unsigned long
  1405. read_spr(int n)
  1406. {
  1407. unsigned int instrs[2];
  1408. unsigned long (*code)(void);
  1409. unsigned long ret = -1UL;
  1410. #ifdef CONFIG_PPC64
  1411. unsigned long opd[3];
  1412. opd[0] = (unsigned long)instrs;
  1413. opd[1] = 0;
  1414. opd[2] = 0;
  1415. code = (unsigned long (*)(void)) opd;
  1416. #else
  1417. code = (unsigned long (*)(void)) instrs;
  1418. #endif
  1419. /* mfspr r3,n; blr */
  1420. instrs[0] = 0x7c6002a6 + ((n & 0x1F) << 16) + ((n & 0x3e0) << 6);
  1421. instrs[1] = 0x4e800020;
  1422. store_inst(instrs);
  1423. store_inst(instrs+1);
  1424. if (setjmp(bus_error_jmp) == 0) {
  1425. catch_memory_errors = 1;
  1426. sync();
  1427. ret = code();
  1428. sync();
  1429. /* wait a little while to see if we get a machine check */
  1430. __delay(200);
  1431. n = size;
  1432. }
  1433. return ret;
  1434. }
  1435. static void
  1436. write_spr(int n, unsigned long val)
  1437. {
  1438. unsigned int instrs[2];
  1439. unsigned long (*code)(unsigned long);
  1440. #ifdef CONFIG_PPC64
  1441. unsigned long opd[3];
  1442. opd[0] = (unsigned long)instrs;
  1443. opd[1] = 0;
  1444. opd[2] = 0;
  1445. code = (unsigned long (*)(unsigned long)) opd;
  1446. #else
  1447. code = (unsigned long (*)(unsigned long)) instrs;
  1448. #endif
  1449. instrs[0] = 0x7c6003a6 + ((n & 0x1F) << 16) + ((n & 0x3e0) << 6);
  1450. instrs[1] = 0x4e800020;
  1451. store_inst(instrs);
  1452. store_inst(instrs+1);
  1453. if (setjmp(bus_error_jmp) == 0) {
  1454. catch_memory_errors = 1;
  1455. sync();
  1456. code(val);
  1457. sync();
  1458. /* wait a little while to see if we get a machine check */
  1459. __delay(200);
  1460. n = size;
  1461. }
  1462. }
  1463. static unsigned long regno;
  1464. extern char exc_prolog;
  1465. extern char dec_exc;
  1466. static void super_regs(void)
  1467. {
  1468. int cmd;
  1469. unsigned long val;
  1470. cmd = skipbl();
  1471. if (cmd == '\n') {
  1472. unsigned long sp, toc;
  1473. asm("mr %0,1" : "=r" (sp) :);
  1474. asm("mr %0,2" : "=r" (toc) :);
  1475. printf("msr = "REG" sprg0= "REG"\n",
  1476. mfmsr(), mfspr(SPRN_SPRG0));
  1477. printf("pvr = "REG" sprg1= "REG"\n",
  1478. mfspr(SPRN_PVR), mfspr(SPRN_SPRG1));
  1479. printf("dec = "REG" sprg2= "REG"\n",
  1480. mfspr(SPRN_DEC), mfspr(SPRN_SPRG2));
  1481. printf("sp = "REG" sprg3= "REG"\n", sp, mfspr(SPRN_SPRG3));
  1482. printf("toc = "REG" dar = "REG"\n", toc, mfspr(SPRN_DAR));
  1483. return;
  1484. }
  1485. scanhex(&regno);
  1486. switch (cmd) {
  1487. case 'w':
  1488. val = read_spr(regno);
  1489. scanhex(&val);
  1490. write_spr(regno, val);
  1491. /* fall through */
  1492. case 'r':
  1493. printf("spr %lx = %lx\n", regno, read_spr(regno));
  1494. break;
  1495. }
  1496. scannl();
  1497. }
  1498. /*
  1499. * Stuff for reading and writing memory safely
  1500. */
  1501. static int
  1502. mread(unsigned long adrs, void *buf, int size)
  1503. {
  1504. volatile int n;
  1505. char *p, *q;
  1506. n = 0;
  1507. if (setjmp(bus_error_jmp) == 0) {
  1508. catch_memory_errors = 1;
  1509. sync();
  1510. p = (char *)adrs;
  1511. q = (char *)buf;
  1512. switch (size) {
  1513. case 2:
  1514. *(u16 *)q = *(u16 *)p;
  1515. break;
  1516. case 4:
  1517. *(u32 *)q = *(u32 *)p;
  1518. break;
  1519. case 8:
  1520. *(u64 *)q = *(u64 *)p;
  1521. break;
  1522. default:
  1523. for( ; n < size; ++n) {
  1524. *q++ = *p++;
  1525. sync();
  1526. }
  1527. }
  1528. sync();
  1529. /* wait a little while to see if we get a machine check */
  1530. __delay(200);
  1531. n = size;
  1532. }
  1533. catch_memory_errors = 0;
  1534. return n;
  1535. }
  1536. static int
  1537. mwrite(unsigned long adrs, void *buf, int size)
  1538. {
  1539. volatile int n;
  1540. char *p, *q;
  1541. n = 0;
  1542. if (setjmp(bus_error_jmp) == 0) {
  1543. catch_memory_errors = 1;
  1544. sync();
  1545. p = (char *) adrs;
  1546. q = (char *) buf;
  1547. switch (size) {
  1548. case 2:
  1549. *(u16 *)p = *(u16 *)q;
  1550. break;
  1551. case 4:
  1552. *(u32 *)p = *(u32 *)q;
  1553. break;
  1554. case 8:
  1555. *(u64 *)p = *(u64 *)q;
  1556. break;
  1557. default:
  1558. for ( ; n < size; ++n) {
  1559. *p++ = *q++;
  1560. sync();
  1561. }
  1562. }
  1563. sync();
  1564. /* wait a little while to see if we get a machine check */
  1565. __delay(200);
  1566. n = size;
  1567. } else {
  1568. printf("*** Error writing address %x\n", adrs + n);
  1569. }
  1570. catch_memory_errors = 0;
  1571. return n;
  1572. }
  1573. static int fault_type;
  1574. static int fault_except;
  1575. static char *fault_chars[] = { "--", "**", "##" };
  1576. static int handle_fault(struct pt_regs *regs)
  1577. {
  1578. fault_except = TRAP(regs);
  1579. switch (TRAP(regs)) {
  1580. case 0x200:
  1581. fault_type = 0;
  1582. break;
  1583. case 0x300:
  1584. case 0x380:
  1585. fault_type = 1;
  1586. break;
  1587. default:
  1588. fault_type = 2;
  1589. }
  1590. longjmp(bus_error_jmp, 1);
  1591. return 0;
  1592. }
  1593. #define SWAP(a, b, t) ((t) = (a), (a) = (b), (b) = (t))
  1594. static void
  1595. byterev(unsigned char *val, int size)
  1596. {
  1597. int t;
  1598. switch (size) {
  1599. case 2:
  1600. SWAP(val[0], val[1], t);
  1601. break;
  1602. case 4:
  1603. SWAP(val[0], val[3], t);
  1604. SWAP(val[1], val[2], t);
  1605. break;
  1606. case 8: /* is there really any use for this? */
  1607. SWAP(val[0], val[7], t);
  1608. SWAP(val[1], val[6], t);
  1609. SWAP(val[2], val[5], t);
  1610. SWAP(val[3], val[4], t);
  1611. break;
  1612. }
  1613. }
  1614. static int brev;
  1615. static int mnoread;
  1616. static char *memex_help_string =
  1617. "Memory examine command usage:\n"
  1618. "m [addr] [flags] examine/change memory\n"
  1619. " addr is optional. will start where left off.\n"
  1620. " flags may include chars from this set:\n"
  1621. " b modify by bytes (default)\n"
  1622. " w modify by words (2 byte)\n"
  1623. " l modify by longs (4 byte)\n"
  1624. " d modify by doubleword (8 byte)\n"
  1625. " r toggle reverse byte order mode\n"
  1626. " n do not read memory (for i/o spaces)\n"
  1627. " . ok to read (default)\n"
  1628. "NOTE: flags are saved as defaults\n"
  1629. "";
  1630. static char *memex_subcmd_help_string =
  1631. "Memory examine subcommands:\n"
  1632. " hexval write this val to current location\n"
  1633. " 'string' write chars from string to this location\n"
  1634. " ' increment address\n"
  1635. " ^ decrement address\n"
  1636. " / increment addr by 0x10. //=0x100, ///=0x1000, etc\n"
  1637. " \\ decrement addr by 0x10. \\\\=0x100, \\\\\\=0x1000, etc\n"
  1638. " ` clear no-read flag\n"
  1639. " ; stay at this addr\n"
  1640. " v change to byte mode\n"
  1641. " w change to word (2 byte) mode\n"
  1642. " l change to long (4 byte) mode\n"
  1643. " u change to doubleword (8 byte) mode\n"
  1644. " m addr change current addr\n"
  1645. " n toggle no-read flag\n"
  1646. " r toggle byte reverse flag\n"
  1647. " < count back up count bytes\n"
  1648. " > count skip forward count bytes\n"
  1649. " x exit this mode\n"
  1650. "";
  1651. static void
  1652. memex(void)
  1653. {
  1654. int cmd, inc, i, nslash;
  1655. unsigned long n;
  1656. unsigned char val[16];
  1657. scanhex((void *)&adrs);
  1658. cmd = skipbl();
  1659. if (cmd == '?') {
  1660. printf(memex_help_string);
  1661. return;
  1662. } else {
  1663. termch = cmd;
  1664. }
  1665. last_cmd = "m\n";
  1666. while ((cmd = skipbl()) != '\n') {
  1667. switch( cmd ){
  1668. case 'b': size = 1; break;
  1669. case 'w': size = 2; break;
  1670. case 'l': size = 4; break;
  1671. case 'd': size = 8; break;
  1672. case 'r': brev = !brev; break;
  1673. case 'n': mnoread = 1; break;
  1674. case '.': mnoread = 0; break;
  1675. }
  1676. }
  1677. if( size <= 0 )
  1678. size = 1;
  1679. else if( size > 8 )
  1680. size = 8;
  1681. for(;;){
  1682. if (!mnoread)
  1683. n = mread(adrs, val, size);
  1684. printf(REG"%c", adrs, brev? 'r': ' ');
  1685. if (!mnoread) {
  1686. if (brev)
  1687. byterev(val, size);
  1688. putchar(' ');
  1689. for (i = 0; i < n; ++i)
  1690. printf("%.2x", val[i]);
  1691. for (; i < size; ++i)
  1692. printf("%s", fault_chars[fault_type]);
  1693. }
  1694. putchar(' ');
  1695. inc = size;
  1696. nslash = 0;
  1697. for(;;){
  1698. if( scanhex(&n) ){
  1699. for (i = 0; i < size; ++i)
  1700. val[i] = n >> (i * 8);
  1701. if (!brev)
  1702. byterev(val, size);
  1703. mwrite(adrs, val, size);
  1704. inc = size;
  1705. }
  1706. cmd = skipbl();
  1707. if (cmd == '\n')
  1708. break;
  1709. inc = 0;
  1710. switch (cmd) {
  1711. case '\'':
  1712. for(;;){
  1713. n = inchar();
  1714. if( n == '\\' )
  1715. n = bsesc();
  1716. else if( n == '\'' )
  1717. break;
  1718. for (i = 0; i < size; ++i)
  1719. val[i] = n >> (i * 8);
  1720. if (!brev)
  1721. byterev(val, size);
  1722. mwrite(adrs, val, size);
  1723. adrs += size;
  1724. }
  1725. adrs -= size;
  1726. inc = size;
  1727. break;
  1728. case ',':
  1729. adrs += size;
  1730. break;
  1731. case '.':
  1732. mnoread = 0;
  1733. break;
  1734. case ';':
  1735. break;
  1736. case 'x':
  1737. case EOF:
  1738. scannl();
  1739. return;
  1740. case 'b':
  1741. case 'v':
  1742. size = 1;
  1743. break;
  1744. case 'w':
  1745. size = 2;
  1746. break;
  1747. case 'l':
  1748. size = 4;
  1749. break;
  1750. case 'u':
  1751. size = 8;
  1752. break;
  1753. case '^':
  1754. adrs -= size;
  1755. break;
  1756. break;
  1757. case '/':
  1758. if (nslash > 0)
  1759. adrs -= 1 << nslash;
  1760. else
  1761. nslash = 0;
  1762. nslash += 4;
  1763. adrs += 1 << nslash;
  1764. break;
  1765. case '\\':
  1766. if (nslash < 0)
  1767. adrs += 1 << -nslash;
  1768. else
  1769. nslash = 0;
  1770. nslash -= 4;
  1771. adrs -= 1 << -nslash;
  1772. break;
  1773. case 'm':
  1774. scanhex((void *)&adrs);
  1775. break;
  1776. case 'n':
  1777. mnoread = 1;
  1778. break;
  1779. case 'r':
  1780. brev = !brev;
  1781. break;
  1782. case '<':
  1783. n = size;
  1784. scanhex(&n);
  1785. adrs -= n;
  1786. break;
  1787. case '>':
  1788. n = size;
  1789. scanhex(&n);
  1790. adrs += n;
  1791. break;
  1792. case '?':
  1793. printf(memex_subcmd_help_string);
  1794. break;
  1795. }
  1796. }
  1797. adrs += inc;
  1798. }
  1799. }
  1800. static int
  1801. bsesc(void)
  1802. {
  1803. int c;
  1804. c = inchar();
  1805. switch( c ){
  1806. case 'n': c = '\n'; break;
  1807. case 'r': c = '\r'; break;
  1808. case 'b': c = '\b'; break;
  1809. case 't': c = '\t'; break;
  1810. }
  1811. return c;
  1812. }
  1813. static void xmon_rawdump (unsigned long adrs, long ndump)
  1814. {
  1815. long n, m, r, nr;
  1816. unsigned char temp[16];
  1817. for (n = ndump; n > 0;) {
  1818. r = n < 16? n: 16;
  1819. nr = mread(adrs, temp, r);
  1820. adrs += nr;
  1821. for (m = 0; m < r; ++m) {
  1822. if (m < nr)
  1823. printf("%.2x", temp[m]);
  1824. else
  1825. printf("%s", fault_chars[fault_type]);
  1826. }
  1827. n -= r;
  1828. if (nr < r)
  1829. break;
  1830. }
  1831. printf("\n");
  1832. }
  1833. #ifdef CONFIG_PPC64
  1834. static void dump_one_paca(int cpu)
  1835. {
  1836. struct paca_struct *p;
  1837. if (setjmp(bus_error_jmp) != 0) {
  1838. printf("*** Error dumping paca for cpu 0x%x!\n", cpu);
  1839. return;
  1840. }
  1841. catch_memory_errors = 1;
  1842. sync();
  1843. p = &paca[cpu];
  1844. printf("paca for cpu 0x%x @ %p:\n", cpu, p);
  1845. printf(" %-*s = %s\n", 16, "possible", cpu_possible(cpu) ? "yes" : "no");
  1846. printf(" %-*s = %s\n", 16, "present", cpu_present(cpu) ? "yes" : "no");
  1847. printf(" %-*s = %s\n", 16, "online", cpu_online(cpu) ? "yes" : "no");
  1848. #define DUMP(paca, name, format) \
  1849. printf(" %-*s = %#-*"format"\t(0x%lx)\n", 16, #name, 18, paca->name, \
  1850. offsetof(struct paca_struct, name));
  1851. DUMP(p, lock_token, "x");
  1852. DUMP(p, paca_index, "x");
  1853. DUMP(p, kernel_toc, "lx");
  1854. DUMP(p, kernelbase, "lx");
  1855. DUMP(p, kernel_msr, "lx");
  1856. #ifdef CONFIG_PPC_STD_MMU_64
  1857. DUMP(p, stab_real, "lx");
  1858. DUMP(p, stab_addr, "lx");
  1859. #endif
  1860. DUMP(p, emergency_sp, "p");
  1861. DUMP(p, data_offset, "lx");
  1862. DUMP(p, hw_cpu_id, "x");
  1863. DUMP(p, cpu_start, "x");
  1864. DUMP(p, kexec_state, "x");
  1865. DUMP(p, __current, "p");
  1866. DUMP(p, kstack, "lx");
  1867. DUMP(p, stab_rr, "lx");
  1868. DUMP(p, saved_r1, "lx");
  1869. DUMP(p, trap_save, "x");
  1870. DUMP(p, soft_enabled, "x");
  1871. DUMP(p, irq_happened, "x");
  1872. DUMP(p, io_sync, "x");
  1873. DUMP(p, irq_work_pending, "x");
  1874. DUMP(p, nap_state_lost, "x");
  1875. #undef DUMP
  1876. catch_memory_errors = 0;
  1877. sync();
  1878. }
  1879. static void dump_all_pacas(void)
  1880. {
  1881. int cpu;
  1882. if (num_possible_cpus() == 0) {
  1883. printf("No possible cpus, use 'dp #' to dump individual cpus\n");
  1884. return;
  1885. }
  1886. for_each_possible_cpu(cpu)
  1887. dump_one_paca(cpu);
  1888. }
  1889. static void dump_pacas(void)
  1890. {
  1891. unsigned long num;
  1892. int c;
  1893. c = inchar();
  1894. if (c == 'a') {
  1895. dump_all_pacas();
  1896. return;
  1897. }
  1898. termch = c; /* Put c back, it wasn't 'a' */
  1899. if (scanhex(&num))
  1900. dump_one_paca(num);
  1901. else
  1902. dump_one_paca(xmon_owner);
  1903. }
  1904. #endif
  1905. #define isxdigit(c) (('0' <= (c) && (c) <= '9') \
  1906. || ('a' <= (c) && (c) <= 'f') \
  1907. || ('A' <= (c) && (c) <= 'F'))
  1908. static void
  1909. dump(void)
  1910. {
  1911. int c;
  1912. c = inchar();
  1913. #ifdef CONFIG_PPC64
  1914. if (c == 'p') {
  1915. dump_pacas();
  1916. return;
  1917. }
  1918. #endif
  1919. if ((isxdigit(c) && c != 'f' && c != 'd') || c == '\n')
  1920. termch = c;
  1921. scanhex((void *)&adrs);
  1922. if (termch != '\n')
  1923. termch = 0;
  1924. if (c == 'i') {
  1925. scanhex(&nidump);
  1926. if (nidump == 0)
  1927. nidump = 16;
  1928. else if (nidump > MAX_DUMP)
  1929. nidump = MAX_DUMP;
  1930. adrs += ppc_inst_dump(adrs, nidump, 1);
  1931. last_cmd = "di\n";
  1932. } else if (c == 'l') {
  1933. dump_log_buf();
  1934. } else if (c == 'r') {
  1935. scanhex(&ndump);
  1936. if (ndump == 0)
  1937. ndump = 64;
  1938. xmon_rawdump(adrs, ndump);
  1939. adrs += ndump;
  1940. last_cmd = "dr\n";
  1941. } else {
  1942. scanhex(&ndump);
  1943. if (ndump == 0)
  1944. ndump = 64;
  1945. else if (ndump > MAX_DUMP)
  1946. ndump = MAX_DUMP;
  1947. prdump(adrs, ndump);
  1948. adrs += ndump;
  1949. last_cmd = "d\n";
  1950. }
  1951. }
  1952. static void
  1953. prdump(unsigned long adrs, long ndump)
  1954. {
  1955. long n, m, c, r, nr;
  1956. unsigned char temp[16];
  1957. for (n = ndump; n > 0;) {
  1958. printf(REG, adrs);
  1959. putchar(' ');
  1960. r = n < 16? n: 16;
  1961. nr = mread(adrs, temp, r);
  1962. adrs += nr;
  1963. for (m = 0; m < r; ++m) {
  1964. if ((m & (sizeof(long) - 1)) == 0 && m > 0)
  1965. putchar(' ');
  1966. if (m < nr)
  1967. printf("%.2x", temp[m]);
  1968. else
  1969. printf("%s", fault_chars[fault_type]);
  1970. }
  1971. for (; m < 16; ++m) {
  1972. if ((m & (sizeof(long) - 1)) == 0)
  1973. putchar(' ');
  1974. printf(" ");
  1975. }
  1976. printf(" |");
  1977. for (m = 0; m < r; ++m) {
  1978. if (m < nr) {
  1979. c = temp[m];
  1980. putchar(' ' <= c && c <= '~'? c: '.');
  1981. } else
  1982. putchar(' ');
  1983. }
  1984. n -= r;
  1985. for (; m < 16; ++m)
  1986. putchar(' ');
  1987. printf("|\n");
  1988. if (nr < r)
  1989. break;
  1990. }
  1991. }
  1992. typedef int (*instruction_dump_func)(unsigned long inst, unsigned long addr);
  1993. static int
  1994. generic_inst_dump(unsigned long adr, long count, int praddr,
  1995. instruction_dump_func dump_func)
  1996. {
  1997. int nr, dotted;
  1998. unsigned long first_adr;
  1999. unsigned long inst, last_inst = 0;
  2000. unsigned char val[4];
  2001. dotted = 0;
  2002. for (first_adr = adr; count > 0; --count, adr += 4) {
  2003. nr = mread(adr, val, 4);
  2004. if (nr == 0) {
  2005. if (praddr) {
  2006. const char *x = fault_chars[fault_type];
  2007. printf(REG" %s%s%s%s\n", adr, x, x, x, x);
  2008. }
  2009. break;
  2010. }
  2011. inst = GETWORD(val);
  2012. if (adr > first_adr && inst == last_inst) {
  2013. if (!dotted) {
  2014. printf(" ...\n");
  2015. dotted = 1;
  2016. }
  2017. continue;
  2018. }
  2019. dotted = 0;
  2020. last_inst = inst;
  2021. if (praddr)
  2022. printf(REG" %.8x", adr, inst);
  2023. printf("\t");
  2024. dump_func(inst, adr);
  2025. printf("\n");
  2026. }
  2027. return adr - first_adr;
  2028. }
  2029. static int
  2030. ppc_inst_dump(unsigned long adr, long count, int praddr)
  2031. {
  2032. return generic_inst_dump(adr, count, praddr, print_insn_powerpc);
  2033. }
  2034. void
  2035. print_address(unsigned long addr)
  2036. {
  2037. xmon_print_symbol(addr, "\t# ", "");
  2038. }
  2039. void
  2040. dump_log_buf(void)
  2041. {
  2042. struct kmsg_dumper dumper = { .active = 1 };
  2043. unsigned char buf[128];
  2044. size_t len;
  2045. if (setjmp(bus_error_jmp) != 0) {
  2046. printf("Error dumping printk buffer!\n");
  2047. return;
  2048. }
  2049. catch_memory_errors = 1;
  2050. sync();
  2051. kmsg_dump_rewind_nolock(&dumper);
  2052. while (kmsg_dump_get_line_nolock(&dumper, false, buf, sizeof(buf), &len)) {
  2053. buf[len] = '\0';
  2054. printf("%s", buf);
  2055. }
  2056. sync();
  2057. /* wait a little while to see if we get a machine check */
  2058. __delay(200);
  2059. catch_memory_errors = 0;
  2060. }
  2061. /*
  2062. * Memory operations - move, set, print differences
  2063. */
  2064. static unsigned long mdest; /* destination address */
  2065. static unsigned long msrc; /* source address */
  2066. static unsigned long mval; /* byte value to set memory to */
  2067. static unsigned long mcount; /* # bytes to affect */
  2068. static unsigned long mdiffs; /* max # differences to print */
  2069. static void
  2070. memops(int cmd)
  2071. {
  2072. scanhex((void *)&mdest);
  2073. if( termch != '\n' )
  2074. termch = 0;
  2075. scanhex((void *)(cmd == 's'? &mval: &msrc));
  2076. if( termch != '\n' )
  2077. termch = 0;
  2078. scanhex((void *)&mcount);
  2079. switch( cmd ){
  2080. case 'm':
  2081. memmove((void *)mdest, (void *)msrc, mcount);
  2082. break;
  2083. case 's':
  2084. memset((void *)mdest, mval, mcount);
  2085. break;
  2086. case 'd':
  2087. if( termch != '\n' )
  2088. termch = 0;
  2089. scanhex((void *)&mdiffs);
  2090. memdiffs((unsigned char *)mdest, (unsigned char *)msrc, mcount, mdiffs);
  2091. break;
  2092. }
  2093. }
  2094. static void
  2095. memdiffs(unsigned char *p1, unsigned char *p2, unsigned nb, unsigned maxpr)
  2096. {
  2097. unsigned n, prt;
  2098. prt = 0;
  2099. for( n = nb; n > 0; --n )
  2100. if( *p1++ != *p2++ )
  2101. if( ++prt <= maxpr )
  2102. printf("%.16x %.2x # %.16x %.2x\n", p1 - 1,
  2103. p1[-1], p2 - 1, p2[-1]);
  2104. if( prt > maxpr )
  2105. printf("Total of %d differences\n", prt);
  2106. }
  2107. static unsigned mend;
  2108. static unsigned mask;
  2109. static void
  2110. memlocate(void)
  2111. {
  2112. unsigned a, n;
  2113. unsigned char val[4];
  2114. last_cmd = "ml";
  2115. scanhex((void *)&mdest);
  2116. if (termch != '\n') {
  2117. termch = 0;
  2118. scanhex((void *)&mend);
  2119. if (termch != '\n') {
  2120. termch = 0;
  2121. scanhex((void *)&mval);
  2122. mask = ~0;
  2123. if (termch != '\n') termch = 0;
  2124. scanhex((void *)&mask);
  2125. }
  2126. }
  2127. n = 0;
  2128. for (a = mdest; a < mend; a += 4) {
  2129. if (mread(a, val, 4) == 4
  2130. && ((GETWORD(val) ^ mval) & mask) == 0) {
  2131. printf("%.16x: %.16x\n", a, GETWORD(val));
  2132. if (++n >= 10)
  2133. break;
  2134. }
  2135. }
  2136. }
  2137. static unsigned long mskip = 0x1000;
  2138. static unsigned long mlim = 0xffffffff;
  2139. static void
  2140. memzcan(void)
  2141. {
  2142. unsigned char v;
  2143. unsigned a;
  2144. int ok, ook;
  2145. scanhex(&mdest);
  2146. if (termch != '\n') termch = 0;
  2147. scanhex(&mskip);
  2148. if (termch != '\n') termch = 0;
  2149. scanhex(&mlim);
  2150. ook = 0;
  2151. for (a = mdest; a < mlim; a += mskip) {
  2152. ok = mread(a, &v, 1);
  2153. if (ok && !ook) {
  2154. printf("%.8x .. ", a);
  2155. } else if (!ok && ook)
  2156. printf("%.8x\n", a - mskip);
  2157. ook = ok;
  2158. if (a + mskip < a)
  2159. break;
  2160. }
  2161. if (ook)
  2162. printf("%.8x\n", a - mskip);
  2163. }
  2164. static void proccall(void)
  2165. {
  2166. unsigned long args[8];
  2167. unsigned long ret;
  2168. int i;
  2169. typedef unsigned long (*callfunc_t)(unsigned long, unsigned long,
  2170. unsigned long, unsigned long, unsigned long,
  2171. unsigned long, unsigned long, unsigned long);
  2172. callfunc_t func;
  2173. if (!scanhex(&adrs))
  2174. return;
  2175. if (termch != '\n')
  2176. termch = 0;
  2177. for (i = 0; i < 8; ++i)
  2178. args[i] = 0;
  2179. for (i = 0; i < 8; ++i) {
  2180. if (!scanhex(&args[i]) || termch == '\n')
  2181. break;
  2182. termch = 0;
  2183. }
  2184. func = (callfunc_t) adrs;
  2185. ret = 0;
  2186. if (setjmp(bus_error_jmp) == 0) {
  2187. catch_memory_errors = 1;
  2188. sync();
  2189. ret = func(args[0], args[1], args[2], args[3],
  2190. args[4], args[5], args[6], args[7]);
  2191. sync();
  2192. printf("return value is %x\n", ret);
  2193. } else {
  2194. printf("*** %x exception occurred\n", fault_except);
  2195. }
  2196. catch_memory_errors = 0;
  2197. }
  2198. /* Input scanning routines */
  2199. int
  2200. skipbl(void)
  2201. {
  2202. int c;
  2203. if( termch != 0 ){
  2204. c = termch;
  2205. termch = 0;
  2206. } else
  2207. c = inchar();
  2208. while( c == ' ' || c == '\t' )
  2209. c = inchar();
  2210. return c;
  2211. }
  2212. #define N_PTREGS 44
  2213. static char *regnames[N_PTREGS] = {
  2214. "r0", "r1", "r2", "r3", "r4", "r5", "r6", "r7",
  2215. "r8", "r9", "r10", "r11", "r12", "r13", "r14", "r15",
  2216. "r16", "r17", "r18", "r19", "r20", "r21", "r22", "r23",
  2217. "r24", "r25", "r26", "r27", "r28", "r29", "r30", "r31",
  2218. "pc", "msr", "or3", "ctr", "lr", "xer", "ccr",
  2219. #ifdef CONFIG_PPC64
  2220. "softe",
  2221. #else
  2222. "mq",
  2223. #endif
  2224. "trap", "dar", "dsisr", "res"
  2225. };
  2226. int
  2227. scanhex(unsigned long *vp)
  2228. {
  2229. int c, d;
  2230. unsigned long v;
  2231. c = skipbl();
  2232. if (c == '%') {
  2233. /* parse register name */
  2234. char regname[8];
  2235. int i;
  2236. for (i = 0; i < sizeof(regname) - 1; ++i) {
  2237. c = inchar();
  2238. if (!isalnum(c)) {
  2239. termch = c;
  2240. break;
  2241. }
  2242. regname[i] = c;
  2243. }
  2244. regname[i] = 0;
  2245. for (i = 0; i < N_PTREGS; ++i) {
  2246. if (strcmp(regnames[i], regname) == 0) {
  2247. if (xmon_regs == NULL) {
  2248. printf("regs not available\n");
  2249. return 0;
  2250. }
  2251. *vp = ((unsigned long *)xmon_regs)[i];
  2252. return 1;
  2253. }
  2254. }
  2255. printf("invalid register name '%%%s'\n", regname);
  2256. return 0;
  2257. }
  2258. /* skip leading "0x" if any */
  2259. if (c == '0') {
  2260. c = inchar();
  2261. if (c == 'x') {
  2262. c = inchar();
  2263. } else {
  2264. d = hexdigit(c);
  2265. if (d == EOF) {
  2266. termch = c;
  2267. *vp = 0;
  2268. return 1;
  2269. }
  2270. }
  2271. } else if (c == '$') {
  2272. int i;
  2273. for (i=0; i<63; i++) {
  2274. c = inchar();
  2275. if (isspace(c)) {
  2276. termch = c;
  2277. break;
  2278. }
  2279. tmpstr[i] = c;
  2280. }
  2281. tmpstr[i++] = 0;
  2282. *vp = 0;
  2283. if (setjmp(bus_error_jmp) == 0) {
  2284. catch_memory_errors = 1;
  2285. sync();
  2286. *vp = kallsyms_lookup_name(tmpstr);
  2287. sync();
  2288. }
  2289. catch_memory_errors = 0;
  2290. if (!(*vp)) {
  2291. printf("unknown symbol '%s'\n", tmpstr);
  2292. return 0;
  2293. }
  2294. return 1;
  2295. }
  2296. d = hexdigit(c);
  2297. if (d == EOF) {
  2298. termch = c;
  2299. return 0;
  2300. }
  2301. v = 0;
  2302. do {
  2303. v = (v << 4) + d;
  2304. c = inchar();
  2305. d = hexdigit(c);
  2306. } while (d != EOF);
  2307. termch = c;
  2308. *vp = v;
  2309. return 1;
  2310. }
  2311. static void
  2312. scannl(void)
  2313. {
  2314. int c;
  2315. c = termch;
  2316. termch = 0;
  2317. while( c != '\n' )
  2318. c = inchar();
  2319. }
  2320. static int hexdigit(int c)
  2321. {
  2322. if( '0' <= c && c <= '9' )
  2323. return c - '0';
  2324. if( 'A' <= c && c <= 'F' )
  2325. return c - ('A' - 10);
  2326. if( 'a' <= c && c <= 'f' )
  2327. return c - ('a' - 10);
  2328. return EOF;
  2329. }
  2330. void
  2331. getstring(char *s, int size)
  2332. {
  2333. int c;
  2334. c = skipbl();
  2335. do {
  2336. if( size > 1 ){
  2337. *s++ = c;
  2338. --size;
  2339. }
  2340. c = inchar();
  2341. } while( c != ' ' && c != '\t' && c != '\n' );
  2342. termch = c;
  2343. *s = 0;
  2344. }
  2345. static char line[256];
  2346. static char *lineptr;
  2347. static void
  2348. flush_input(void)
  2349. {
  2350. lineptr = NULL;
  2351. }
  2352. static int
  2353. inchar(void)
  2354. {
  2355. if (lineptr == NULL || *lineptr == 0) {
  2356. if (xmon_gets(line, sizeof(line)) == NULL) {
  2357. lineptr = NULL;
  2358. return EOF;
  2359. }
  2360. lineptr = line;
  2361. }
  2362. return *lineptr++;
  2363. }
  2364. static void
  2365. take_input(char *str)
  2366. {
  2367. lineptr = str;
  2368. }
  2369. static void
  2370. symbol_lookup(void)
  2371. {
  2372. int type = inchar();
  2373. unsigned long addr;
  2374. static char tmp[64];
  2375. switch (type) {
  2376. case 'a':
  2377. if (scanhex(&addr))
  2378. xmon_print_symbol(addr, ": ", "\n");
  2379. termch = 0;
  2380. break;
  2381. case 's':
  2382. getstring(tmp, 64);
  2383. if (setjmp(bus_error_jmp) == 0) {
  2384. catch_memory_errors = 1;
  2385. sync();
  2386. addr = kallsyms_lookup_name(tmp);
  2387. if (addr)
  2388. printf("%s: %lx\n", tmp, addr);
  2389. else
  2390. printf("Symbol '%s' not found.\n", tmp);
  2391. sync();
  2392. }
  2393. catch_memory_errors = 0;
  2394. termch = 0;
  2395. break;
  2396. }
  2397. }
  2398. /* Print an address in numeric and symbolic form (if possible) */
  2399. static void xmon_print_symbol(unsigned long address, const char *mid,
  2400. const char *after)
  2401. {
  2402. char *modname;
  2403. const char *name = NULL;
  2404. unsigned long offset, size;
  2405. printf(REG, address);
  2406. if (setjmp(bus_error_jmp) == 0) {
  2407. catch_memory_errors = 1;
  2408. sync();
  2409. name = kallsyms_lookup(address, &size, &offset, &modname,
  2410. tmpstr);
  2411. sync();
  2412. /* wait a little while to see if we get a machine check */
  2413. __delay(200);
  2414. }
  2415. catch_memory_errors = 0;
  2416. if (name) {
  2417. printf("%s%s+%#lx/%#lx", mid, name, offset, size);
  2418. if (modname)
  2419. printf(" [%s]", modname);
  2420. }
  2421. printf("%s", after);
  2422. }
  2423. #ifdef CONFIG_PPC_BOOK3S_64
  2424. static void dump_slb(void)
  2425. {
  2426. int i;
  2427. unsigned long esid,vsid,valid;
  2428. unsigned long llp;
  2429. printf("SLB contents of cpu %x\n", smp_processor_id());
  2430. for (i = 0; i < mmu_slb_size; i++) {
  2431. asm volatile("slbmfee %0,%1" : "=r" (esid) : "r" (i));
  2432. asm volatile("slbmfev %0,%1" : "=r" (vsid) : "r" (i));
  2433. valid = (esid & SLB_ESID_V);
  2434. if (valid | esid | vsid) {
  2435. printf("%02d %016lx %016lx", i, esid, vsid);
  2436. if (valid) {
  2437. llp = vsid & SLB_VSID_LLP;
  2438. if (vsid & SLB_VSID_B_1T) {
  2439. printf(" 1T ESID=%9lx VSID=%13lx LLP:%3lx \n",
  2440. GET_ESID_1T(esid),
  2441. (vsid & ~SLB_VSID_B) >> SLB_VSID_SHIFT_1T,
  2442. llp);
  2443. } else {
  2444. printf(" 256M ESID=%9lx VSID=%13lx LLP:%3lx \n",
  2445. GET_ESID(esid),
  2446. (vsid & ~SLB_VSID_B) >> SLB_VSID_SHIFT,
  2447. llp);
  2448. }
  2449. } else
  2450. printf("\n");
  2451. }
  2452. }
  2453. }
  2454. static void dump_stab(void)
  2455. {
  2456. int i;
  2457. unsigned long *tmp = (unsigned long *)local_paca->stab_addr;
  2458. printf("Segment table contents of cpu %x\n", smp_processor_id());
  2459. for (i = 0; i < PAGE_SIZE/16; i++) {
  2460. unsigned long a, b;
  2461. a = *tmp++;
  2462. b = *tmp++;
  2463. if (a || b) {
  2464. printf("%03d %016lx ", i, a);
  2465. printf("%016lx\n", b);
  2466. }
  2467. }
  2468. }
  2469. void dump_segments(void)
  2470. {
  2471. if (mmu_has_feature(MMU_FTR_SLB))
  2472. dump_slb();
  2473. else
  2474. dump_stab();
  2475. }
  2476. #endif
  2477. #ifdef CONFIG_PPC_STD_MMU_32
  2478. void dump_segments(void)
  2479. {
  2480. int i;
  2481. printf("sr0-15 =");
  2482. for (i = 0; i < 16; ++i)
  2483. printf(" %x", mfsrin(i));
  2484. printf("\n");
  2485. }
  2486. #endif
  2487. #ifdef CONFIG_44x
  2488. static void dump_tlb_44x(void)
  2489. {
  2490. int i;
  2491. for (i = 0; i < PPC44x_TLB_SIZE; i++) {
  2492. unsigned long w0,w1,w2;
  2493. asm volatile("tlbre %0,%1,0" : "=r" (w0) : "r" (i));
  2494. asm volatile("tlbre %0,%1,1" : "=r" (w1) : "r" (i));
  2495. asm volatile("tlbre %0,%1,2" : "=r" (w2) : "r" (i));
  2496. printf("[%02x] %08x %08x %08x ", i, w0, w1, w2);
  2497. if (w0 & PPC44x_TLB_VALID) {
  2498. printf("V %08x -> %01x%08x %c%c%c%c%c",
  2499. w0 & PPC44x_TLB_EPN_MASK,
  2500. w1 & PPC44x_TLB_ERPN_MASK,
  2501. w1 & PPC44x_TLB_RPN_MASK,
  2502. (w2 & PPC44x_TLB_W) ? 'W' : 'w',
  2503. (w2 & PPC44x_TLB_I) ? 'I' : 'i',
  2504. (w2 & PPC44x_TLB_M) ? 'M' : 'm',
  2505. (w2 & PPC44x_TLB_G) ? 'G' : 'g',
  2506. (w2 & PPC44x_TLB_E) ? 'E' : 'e');
  2507. }
  2508. printf("\n");
  2509. }
  2510. }
  2511. #endif /* CONFIG_44x */
  2512. #ifdef CONFIG_PPC_BOOK3E
  2513. static void dump_tlb_book3e(void)
  2514. {
  2515. u32 mmucfg, pidmask, lpidmask;
  2516. u64 ramask;
  2517. int i, tlb, ntlbs, pidsz, lpidsz, rasz, lrat = 0;
  2518. int mmu_version;
  2519. static const char *pgsz_names[] = {
  2520. " 1K",
  2521. " 2K",
  2522. " 4K",
  2523. " 8K",
  2524. " 16K",
  2525. " 32K",
  2526. " 64K",
  2527. "128K",
  2528. "256K",
  2529. "512K",
  2530. " 1M",
  2531. " 2M",
  2532. " 4M",
  2533. " 8M",
  2534. " 16M",
  2535. " 32M",
  2536. " 64M",
  2537. "128M",
  2538. "256M",
  2539. "512M",
  2540. " 1G",
  2541. " 2G",
  2542. " 4G",
  2543. " 8G",
  2544. " 16G",
  2545. " 32G",
  2546. " 64G",
  2547. "128G",
  2548. "256G",
  2549. "512G",
  2550. " 1T",
  2551. " 2T",
  2552. };
  2553. /* Gather some infos about the MMU */
  2554. mmucfg = mfspr(SPRN_MMUCFG);
  2555. mmu_version = (mmucfg & 3) + 1;
  2556. ntlbs = ((mmucfg >> 2) & 3) + 1;
  2557. pidsz = ((mmucfg >> 6) & 0x1f) + 1;
  2558. lpidsz = (mmucfg >> 24) & 0xf;
  2559. rasz = (mmucfg >> 16) & 0x7f;
  2560. if ((mmu_version > 1) && (mmucfg & 0x10000))
  2561. lrat = 1;
  2562. printf("Book3E MMU MAV=%d.0,%d TLBs,%d-bit PID,%d-bit LPID,%d-bit RA\n",
  2563. mmu_version, ntlbs, pidsz, lpidsz, rasz);
  2564. pidmask = (1ul << pidsz) - 1;
  2565. lpidmask = (1ul << lpidsz) - 1;
  2566. ramask = (1ull << rasz) - 1;
  2567. for (tlb = 0; tlb < ntlbs; tlb++) {
  2568. u32 tlbcfg;
  2569. int nent, assoc, new_cc = 1;
  2570. printf("TLB %d:\n------\n", tlb);
  2571. switch(tlb) {
  2572. case 0:
  2573. tlbcfg = mfspr(SPRN_TLB0CFG);
  2574. break;
  2575. case 1:
  2576. tlbcfg = mfspr(SPRN_TLB1CFG);
  2577. break;
  2578. case 2:
  2579. tlbcfg = mfspr(SPRN_TLB2CFG);
  2580. break;
  2581. case 3:
  2582. tlbcfg = mfspr(SPRN_TLB3CFG);
  2583. break;
  2584. default:
  2585. printf("Unsupported TLB number !\n");
  2586. continue;
  2587. }
  2588. nent = tlbcfg & 0xfff;
  2589. assoc = (tlbcfg >> 24) & 0xff;
  2590. for (i = 0; i < nent; i++) {
  2591. u32 mas0 = MAS0_TLBSEL(tlb);
  2592. u32 mas1 = MAS1_TSIZE(BOOK3E_PAGESZ_4K);
  2593. u64 mas2 = 0;
  2594. u64 mas7_mas3;
  2595. int esel = i, cc = i;
  2596. if (assoc != 0) {
  2597. cc = i / assoc;
  2598. esel = i % assoc;
  2599. mas2 = cc * 0x1000;
  2600. }
  2601. mas0 |= MAS0_ESEL(esel);
  2602. mtspr(SPRN_MAS0, mas0);
  2603. mtspr(SPRN_MAS1, mas1);
  2604. mtspr(SPRN_MAS2, mas2);
  2605. asm volatile("tlbre 0,0,0" : : : "memory");
  2606. mas1 = mfspr(SPRN_MAS1);
  2607. mas2 = mfspr(SPRN_MAS2);
  2608. mas7_mas3 = mfspr(SPRN_MAS7_MAS3);
  2609. if (assoc && (i % assoc) == 0)
  2610. new_cc = 1;
  2611. if (!(mas1 & MAS1_VALID))
  2612. continue;
  2613. if (assoc == 0)
  2614. printf("%04x- ", i);
  2615. else if (new_cc)
  2616. printf("%04x-%c", cc, 'A' + esel);
  2617. else
  2618. printf(" |%c", 'A' + esel);
  2619. new_cc = 0;
  2620. printf(" %016llx %04x %s %c%c AS%c",
  2621. mas2 & ~0x3ffull,
  2622. (mas1 >> 16) & 0x3fff,
  2623. pgsz_names[(mas1 >> 7) & 0x1f],
  2624. mas1 & MAS1_IND ? 'I' : ' ',
  2625. mas1 & MAS1_IPROT ? 'P' : ' ',
  2626. mas1 & MAS1_TS ? '1' : '0');
  2627. printf(" %c%c%c%c%c%c%c",
  2628. mas2 & MAS2_X0 ? 'a' : ' ',
  2629. mas2 & MAS2_X1 ? 'v' : ' ',
  2630. mas2 & MAS2_W ? 'w' : ' ',
  2631. mas2 & MAS2_I ? 'i' : ' ',
  2632. mas2 & MAS2_M ? 'm' : ' ',
  2633. mas2 & MAS2_G ? 'g' : ' ',
  2634. mas2 & MAS2_E ? 'e' : ' ');
  2635. printf(" %016llx", mas7_mas3 & ramask & ~0x7ffull);
  2636. if (mas1 & MAS1_IND)
  2637. printf(" %s\n",
  2638. pgsz_names[(mas7_mas3 >> 1) & 0x1f]);
  2639. else
  2640. printf(" U%c%c%c S%c%c%c\n",
  2641. mas7_mas3 & MAS3_UX ? 'x' : ' ',
  2642. mas7_mas3 & MAS3_UW ? 'w' : ' ',
  2643. mas7_mas3 & MAS3_UR ? 'r' : ' ',
  2644. mas7_mas3 & MAS3_SX ? 'x' : ' ',
  2645. mas7_mas3 & MAS3_SW ? 'w' : ' ',
  2646. mas7_mas3 & MAS3_SR ? 'r' : ' ');
  2647. }
  2648. }
  2649. }
  2650. #endif /* CONFIG_PPC_BOOK3E */
  2651. static void xmon_init(int enable)
  2652. {
  2653. if (enable) {
  2654. __debugger = xmon;
  2655. __debugger_ipi = xmon_ipi;
  2656. __debugger_bpt = xmon_bpt;
  2657. __debugger_sstep = xmon_sstep;
  2658. __debugger_iabr_match = xmon_iabr_match;
  2659. __debugger_dabr_match = xmon_dabr_match;
  2660. __debugger_fault_handler = xmon_fault_handler;
  2661. } else {
  2662. __debugger = NULL;
  2663. __debugger_ipi = NULL;
  2664. __debugger_bpt = NULL;
  2665. __debugger_sstep = NULL;
  2666. __debugger_iabr_match = NULL;
  2667. __debugger_dabr_match = NULL;
  2668. __debugger_fault_handler = NULL;
  2669. }
  2670. xmon_map_scc();
  2671. }
  2672. #ifdef CONFIG_MAGIC_SYSRQ
  2673. static void sysrq_handle_xmon(int key)
  2674. {
  2675. /* ensure xmon is enabled */
  2676. xmon_init(1);
  2677. debugger(get_irq_regs());
  2678. }
  2679. static struct sysrq_key_op sysrq_xmon_op = {
  2680. .handler = sysrq_handle_xmon,
  2681. .help_msg = "Xmon",
  2682. .action_msg = "Entering xmon",
  2683. };
  2684. static int __init setup_xmon_sysrq(void)
  2685. {
  2686. register_sysrq_key('x', &sysrq_xmon_op);
  2687. return 0;
  2688. }
  2689. __initcall(setup_xmon_sysrq);
  2690. #endif /* CONFIG_MAGIC_SYSRQ */
  2691. static int __initdata xmon_early, xmon_off;
  2692. static int __init early_parse_xmon(char *p)
  2693. {
  2694. if (!p || strncmp(p, "early", 5) == 0) {
  2695. /* just "xmon" is equivalent to "xmon=early" */
  2696. xmon_init(1);
  2697. xmon_early = 1;
  2698. } else if (strncmp(p, "on", 2) == 0)
  2699. xmon_init(1);
  2700. else if (strncmp(p, "off", 3) == 0)
  2701. xmon_off = 1;
  2702. else if (strncmp(p, "nobt", 4) == 0)
  2703. xmon_no_auto_backtrace = 1;
  2704. else
  2705. return 1;
  2706. return 0;
  2707. }
  2708. early_param("xmon", early_parse_xmon);
  2709. void __init xmon_setup(void)
  2710. {
  2711. #ifdef CONFIG_XMON_DEFAULT
  2712. if (!xmon_off)
  2713. xmon_init(1);
  2714. #endif
  2715. if (xmon_early)
  2716. debugger(NULL);
  2717. }
  2718. #ifdef CONFIG_SPU_BASE
  2719. struct spu_info {
  2720. struct spu *spu;
  2721. u64 saved_mfc_sr1_RW;
  2722. u32 saved_spu_runcntl_RW;
  2723. unsigned long dump_addr;
  2724. u8 stopped_ok;
  2725. };
  2726. #define XMON_NUM_SPUS 16 /* Enough for current hardware */
  2727. static struct spu_info spu_info[XMON_NUM_SPUS];
  2728. void xmon_register_spus(struct list_head *list)
  2729. {
  2730. struct spu *spu;
  2731. list_for_each_entry(spu, list, full_list) {
  2732. if (spu->number >= XMON_NUM_SPUS) {
  2733. WARN_ON(1);
  2734. continue;
  2735. }
  2736. spu_info[spu->number].spu = spu;
  2737. spu_info[spu->number].stopped_ok = 0;
  2738. spu_info[spu->number].dump_addr = (unsigned long)
  2739. spu_info[spu->number].spu->local_store;
  2740. }
  2741. }
  2742. static void stop_spus(void)
  2743. {
  2744. struct spu *spu;
  2745. int i;
  2746. u64 tmp;
  2747. for (i = 0; i < XMON_NUM_SPUS; i++) {
  2748. if (!spu_info[i].spu)
  2749. continue;
  2750. if (setjmp(bus_error_jmp) == 0) {
  2751. catch_memory_errors = 1;
  2752. sync();
  2753. spu = spu_info[i].spu;
  2754. spu_info[i].saved_spu_runcntl_RW =
  2755. in_be32(&spu->problem->spu_runcntl_RW);
  2756. tmp = spu_mfc_sr1_get(spu);
  2757. spu_info[i].saved_mfc_sr1_RW = tmp;
  2758. tmp &= ~MFC_STATE1_MASTER_RUN_CONTROL_MASK;
  2759. spu_mfc_sr1_set(spu, tmp);
  2760. sync();
  2761. __delay(200);
  2762. spu_info[i].stopped_ok = 1;
  2763. printf("Stopped spu %.2d (was %s)\n", i,
  2764. spu_info[i].saved_spu_runcntl_RW ?
  2765. "running" : "stopped");
  2766. } else {
  2767. catch_memory_errors = 0;
  2768. printf("*** Error stopping spu %.2d\n", i);
  2769. }
  2770. catch_memory_errors = 0;
  2771. }
  2772. }
  2773. static void restart_spus(void)
  2774. {
  2775. struct spu *spu;
  2776. int i;
  2777. for (i = 0; i < XMON_NUM_SPUS; i++) {
  2778. if (!spu_info[i].spu)
  2779. continue;
  2780. if (!spu_info[i].stopped_ok) {
  2781. printf("*** Error, spu %d was not successfully stopped"
  2782. ", not restarting\n", i);
  2783. continue;
  2784. }
  2785. if (setjmp(bus_error_jmp) == 0) {
  2786. catch_memory_errors = 1;
  2787. sync();
  2788. spu = spu_info[i].spu;
  2789. spu_mfc_sr1_set(spu, spu_info[i].saved_mfc_sr1_RW);
  2790. out_be32(&spu->problem->spu_runcntl_RW,
  2791. spu_info[i].saved_spu_runcntl_RW);
  2792. sync();
  2793. __delay(200);
  2794. printf("Restarted spu %.2d\n", i);
  2795. } else {
  2796. catch_memory_errors = 0;
  2797. printf("*** Error restarting spu %.2d\n", i);
  2798. }
  2799. catch_memory_errors = 0;
  2800. }
  2801. }
  2802. #define DUMP_WIDTH 23
  2803. #define DUMP_VALUE(format, field, value) \
  2804. do { \
  2805. if (setjmp(bus_error_jmp) == 0) { \
  2806. catch_memory_errors = 1; \
  2807. sync(); \
  2808. printf(" %-*s = "format"\n", DUMP_WIDTH, \
  2809. #field, value); \
  2810. sync(); \
  2811. __delay(200); \
  2812. } else { \
  2813. catch_memory_errors = 0; \
  2814. printf(" %-*s = *** Error reading field.\n", \
  2815. DUMP_WIDTH, #field); \
  2816. } \
  2817. catch_memory_errors = 0; \
  2818. } while (0)
  2819. #define DUMP_FIELD(obj, format, field) \
  2820. DUMP_VALUE(format, field, obj->field)
  2821. static void dump_spu_fields(struct spu *spu)
  2822. {
  2823. printf("Dumping spu fields at address %p:\n", spu);
  2824. DUMP_FIELD(spu, "0x%x", number);
  2825. DUMP_FIELD(spu, "%s", name);
  2826. DUMP_FIELD(spu, "0x%lx", local_store_phys);
  2827. DUMP_FIELD(spu, "0x%p", local_store);
  2828. DUMP_FIELD(spu, "0x%lx", ls_size);
  2829. DUMP_FIELD(spu, "0x%x", node);
  2830. DUMP_FIELD(spu, "0x%lx", flags);
  2831. DUMP_FIELD(spu, "%d", class_0_pending);
  2832. DUMP_FIELD(spu, "0x%lx", class_0_dar);
  2833. DUMP_FIELD(spu, "0x%lx", class_1_dar);
  2834. DUMP_FIELD(spu, "0x%lx", class_1_dsisr);
  2835. DUMP_FIELD(spu, "0x%lx", irqs[0]);
  2836. DUMP_FIELD(spu, "0x%lx", irqs[1]);
  2837. DUMP_FIELD(spu, "0x%lx", irqs[2]);
  2838. DUMP_FIELD(spu, "0x%x", slb_replace);
  2839. DUMP_FIELD(spu, "%d", pid);
  2840. DUMP_FIELD(spu, "0x%p", mm);
  2841. DUMP_FIELD(spu, "0x%p", ctx);
  2842. DUMP_FIELD(spu, "0x%p", rq);
  2843. DUMP_FIELD(spu, "0x%p", timestamp);
  2844. DUMP_FIELD(spu, "0x%lx", problem_phys);
  2845. DUMP_FIELD(spu, "0x%p", problem);
  2846. DUMP_VALUE("0x%x", problem->spu_runcntl_RW,
  2847. in_be32(&spu->problem->spu_runcntl_RW));
  2848. DUMP_VALUE("0x%x", problem->spu_status_R,
  2849. in_be32(&spu->problem->spu_status_R));
  2850. DUMP_VALUE("0x%x", problem->spu_npc_RW,
  2851. in_be32(&spu->problem->spu_npc_RW));
  2852. DUMP_FIELD(spu, "0x%p", priv2);
  2853. DUMP_FIELD(spu, "0x%p", pdata);
  2854. }
  2855. int
  2856. spu_inst_dump(unsigned long adr, long count, int praddr)
  2857. {
  2858. return generic_inst_dump(adr, count, praddr, print_insn_spu);
  2859. }
  2860. static void dump_spu_ls(unsigned long num, int subcmd)
  2861. {
  2862. unsigned long offset, addr, ls_addr;
  2863. if (setjmp(bus_error_jmp) == 0) {
  2864. catch_memory_errors = 1;
  2865. sync();
  2866. ls_addr = (unsigned long)spu_info[num].spu->local_store;
  2867. sync();
  2868. __delay(200);
  2869. } else {
  2870. catch_memory_errors = 0;
  2871. printf("*** Error: accessing spu info for spu %d\n", num);
  2872. return;
  2873. }
  2874. catch_memory_errors = 0;
  2875. if (scanhex(&offset))
  2876. addr = ls_addr + offset;
  2877. else
  2878. addr = spu_info[num].dump_addr;
  2879. if (addr >= ls_addr + LS_SIZE) {
  2880. printf("*** Error: address outside of local store\n");
  2881. return;
  2882. }
  2883. switch (subcmd) {
  2884. case 'i':
  2885. addr += spu_inst_dump(addr, 16, 1);
  2886. last_cmd = "sdi\n";
  2887. break;
  2888. default:
  2889. prdump(addr, 64);
  2890. addr += 64;
  2891. last_cmd = "sd\n";
  2892. break;
  2893. }
  2894. spu_info[num].dump_addr = addr;
  2895. }
  2896. static int do_spu_cmd(void)
  2897. {
  2898. static unsigned long num = 0;
  2899. int cmd, subcmd = 0;
  2900. cmd = inchar();
  2901. switch (cmd) {
  2902. case 's':
  2903. stop_spus();
  2904. break;
  2905. case 'r':
  2906. restart_spus();
  2907. break;
  2908. case 'd':
  2909. subcmd = inchar();
  2910. if (isxdigit(subcmd) || subcmd == '\n')
  2911. termch = subcmd;
  2912. case 'f':
  2913. scanhex(&num);
  2914. if (num >= XMON_NUM_SPUS || !spu_info[num].spu) {
  2915. printf("*** Error: invalid spu number\n");
  2916. return 0;
  2917. }
  2918. switch (cmd) {
  2919. case 'f':
  2920. dump_spu_fields(spu_info[num].spu);
  2921. break;
  2922. default:
  2923. dump_spu_ls(num, subcmd);
  2924. break;
  2925. }
  2926. break;
  2927. default:
  2928. return -1;
  2929. }
  2930. return 0;
  2931. }
  2932. #else /* ! CONFIG_SPU_BASE */
  2933. static int do_spu_cmd(void)
  2934. {
  2935. return -1;
  2936. }
  2937. #endif