xmon.c 68 KB

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