xmon.c 68 KB

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