Commit | Line | Data |
---|---|---|
920dae64 AT |
1 | # ========== Copyright Header Begin ========================================== |
2 | # | |
3 | # OpenSPARC T2 Processor File: SamFE.py | |
4 | # Copyright (c) 2006 Sun Microsystems, Inc. All Rights Reserved. | |
5 | # DO NOT ALTER OR REMOVE COPYRIGHT NOTICES. | |
6 | # | |
7 | # The above named program is free software; you can redistribute it and/or | |
8 | # modify it under the terms of the GNU General Public | |
9 | # License version 2 as published by the Free Software Foundation. | |
10 | # | |
11 | # The above named program is distributed in the hope that it will be | |
12 | # useful, but WITHOUT ANY WARRANTY; without even the implied warranty of | |
13 | # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | |
14 | # General Public License for more details. | |
15 | # | |
16 | # You should have received a copy of the GNU General Public | |
17 | # License along with this work; if not, write to the Free Software | |
18 | # Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA. | |
19 | # | |
20 | # ========== Copyright Header End ============================================ | |
21 | """Utilities needed to emulate Python's interactive interpreter. | |
22 | """ | |
23 | # | |
24 | # copied from RiesCode.py, for cosim & sam environment | |
25 | # | |
26 | ||
27 | import os, re, string, sys, time, types | |
28 | ||
29 | from Pfe_Conversion import * | |
30 | from Pfe_Assembler import * | |
31 | ||
32 | no_sam = 0 | |
33 | try: | |
34 | import sam | |
35 | except: | |
36 | no_sam = 1 | |
37 | ||
38 | import traceback | |
39 | from codeop import CommandCompiler, compile_command | |
40 | ||
41 | __all__ = ["InteractiveInterpreter", "InteractiveConsole", "interact", | |
42 | "compile_command"] | |
43 | ||
44 | # constants | |
45 | PLI_RUN = 'pli-run' | |
46 | QUIT = 'quit' | |
47 | RUN_PYTHON_FILE = 'run-python-file' | |
48 | ||
49 | DONE = 1 | |
50 | DONE_NOT = 0 | |
51 | ||
52 | # when 1, ignore any exception associated with a wrong python statement | |
53 | IGNORE_EXCEPT = 0 | |
54 | ||
55 | SIM = 'sim' | |
56 | RSYS = 'rsys' | |
57 | CPU = 'cpu' | |
58 | CORE = 'core' | |
59 | UCORE = 'ucore' | |
60 | STRAND = 'strand' | |
61 | ||
62 | # regular expression used to catch execfile() command | |
63 | execfileRE = re.compile("^execfile(\s)*\([^\(\)]+\)$") | |
64 | NEW_CMD_RE = re.compile('^@?new_command\s*\(') | |
65 | DIGITAL_RE = re.compile('^[0-9]+$') | |
66 | ||
67 | RC_SYS_CONFIG_OBJ = 'config0' | |
68 | RC_PROMPT = 'prompt' | |
69 | RC_MEM_IMAGE = 'mem_image' | |
70 | RC_COMMAND = 'command' | |
71 | RC_COMMAND_EXT = 'command_ext' | |
72 | RC_FUNC_DEF = 'func_def' | |
73 | ||
74 | ||
75 | def softspace(file, newvalue): | |
76 | oldvalue = 0 | |
77 | try: | |
78 | oldvalue = file.softspace | |
79 | except AttributeError: | |
80 | pass | |
81 | try: | |
82 | file.softspace = newvalue | |
83 | except (AttributeError, TypeError): | |
84 | # "attribute-less object" or "read-only attributes" | |
85 | pass | |
86 | return oldvalue | |
87 | ||
88 | class InteractiveInterpreter: | |
89 | """Base class for InteractiveConsole. | |
90 | ||
91 | This class deals with parsing and interpreter state (the user's | |
92 | namespace); it doesn't deal with input buffering or prompting or | |
93 | input file naming (the filename is always passed in explicitly). | |
94 | ||
95 | """ | |
96 | ||
97 | def __init__(self, locals=None): | |
98 | """Constructor. | |
99 | ||
100 | The optional 'locals' argument specifies the dictionary in | |
101 | which code will be executed; it defaults to a newly created | |
102 | dictionary with key "__name__" set to "__console__" and key | |
103 | "__doc__" set to None. | |
104 | ||
105 | """ | |
106 | if locals is None: | |
107 | locals = {"__name__": "__console__", "__doc__": None} | |
108 | self.locals = locals | |
109 | self.compile = CommandCompiler() | |
110 | ||
111 | def runsource(self, source, filename="<input>", symbol="single"): | |
112 | """Compile and run some source in the interpreter. | |
113 | ||
114 | Arguments are as for compile_command(). | |
115 | ||
116 | One several things can happen: | |
117 | ||
118 | 1) The input is incorrect; compile_command() raised an | |
119 | exception (SyntaxError or OverflowError). A syntax traceback | |
120 | will be printed by calling the showsyntaxerror() method. | |
121 | ||
122 | 2) The input is incomplete, and more input is required; | |
123 | compile_command() returned None. Nothing happens. | |
124 | ||
125 | 3) The input is complete; compile_command() returned a code | |
126 | object. The code is executed by calling self.runcode() (which | |
127 | also handles run-time exceptions, except for SystemExit). | |
128 | ||
129 | The return value is 1 in case 2, 0 in the other cases (unless | |
130 | an exception is raised). The return value can be used to | |
131 | decide whether to use sys.ps1 or sys.ps2 to prompt the next | |
132 | line. | |
133 | ||
134 | """ | |
135 | global IGNORE_EXCEPT | |
136 | ||
137 | try: | |
138 | code = self.compile(source, filename, symbol) | |
139 | except (OverflowError, SyntaxError, ValueError): | |
140 | # Case 1 | |
141 | if IGNORE_EXCEPT != 1: | |
142 | self.showsyntaxerror(filename) | |
143 | else: | |
144 | raise | |
145 | return 0 | |
146 | ||
147 | if code is None: | |
148 | # Case 2 | |
149 | return 1 | |
150 | ||
151 | # Case 3 | |
152 | self.runcode(code) | |
153 | return 0 | |
154 | ||
155 | def runcode(self, code): | |
156 | """Execute a code object. | |
157 | ||
158 | When an exception occurs, self.showtraceback() is called to | |
159 | display a traceback. All exceptions are caught except | |
160 | SystemExit, which is reraised. | |
161 | ||
162 | A note about KeyboardInterrupt: this exception may occur | |
163 | elsewhere in this code, and may not always be caught. The | |
164 | caller should be prepared to deal with it. | |
165 | ||
166 | """ | |
167 | global IGNORE_EXCEPT | |
168 | ||
169 | try: | |
170 | exec code in self.locals | |
171 | except SystemExit: | |
172 | sys.stderr.write("# %s: end riesling frontend\n" % (time.ctime())) | |
173 | raise | |
174 | ## except AttributeError: | |
175 | ## raise | |
176 | except: | |
177 | if IGNORE_EXCEPT != 1: | |
178 | self.showtraceback() | |
179 | else: | |
180 | raise | |
181 | else: | |
182 | if softspace(sys.stdout, 0): | |
183 | ||
184 | ||
185 | def showsyntaxerror(self, filename=None): | |
186 | """Display the syntax error that just occurred. | |
187 | ||
188 | This doesn't display a stack trace because there isn't one. | |
189 | ||
190 | If a filename is given, it is stuffed in the exception instead | |
191 | of what was there before (because Python's parser always uses | |
192 | "<string>" when reading from a string). | |
193 | ||
194 | The output is written by self.write(), below. | |
195 | ||
196 | """ | |
197 | type, value, sys.last_traceback = sys.exc_info() | |
198 | sys.last_type = type | |
199 | sys.last_value = value | |
200 | if filename and type is SyntaxError: | |
201 | # Work hard to stuff the correct filename in the exception | |
202 | try: | |
203 | msg, (dummy_filename, lineno, offset, line) = value | |
204 | except: | |
205 | # Not the format we expect; leave it alone | |
206 | pass | |
207 | else: | |
208 | # Stuff in the right filename | |
209 | try: | |
210 | # Assume SyntaxError is a class exception | |
211 | value = SyntaxError(msg, (filename, lineno, offset, line)) | |
212 | except: | |
213 | # If that failed, assume SyntaxError is a string | |
214 | value = msg, (filename, lineno, offset, line) | |
215 | sys.last_value = value | |
216 | list = traceback.format_exception_only(type, value) | |
217 | map(self.write, list) | |
218 | ||
219 | def showtraceback(self): | |
220 | """Display the exception that just occurred. | |
221 | ||
222 | We remove the first stack item because it is our own code. | |
223 | ||
224 | The output is written by self.write(), below. | |
225 | ||
226 | """ | |
227 | try: | |
228 | type, value, tb = sys.exc_info() | |
229 | sys.last_type = type | |
230 | sys.last_value = value | |
231 | sys.last_traceback = tb | |
232 | tblist = traceback.extract_tb(tb) | |
233 | del tblist[:1] | |
234 | list = traceback.format_list(tblist) | |
235 | if list: | |
236 | list.insert(0, "Traceback (most recent call last):\n") | |
237 | list[len(list):] = traceback.format_exception_only(type, value) | |
238 | finally: | |
239 | tblist = tb = None | |
240 | map(self.write, list) | |
241 | ||
242 | def write(self, data): | |
243 | """Write a string. | |
244 | ||
245 | The base implementation writes to sys.stderr; a subclass may | |
246 | replace this with a different implementation. | |
247 | ||
248 | """ | |
249 | sys.stderr.write(data) | |
250 | ||
251 | ||
252 | class InteractiveConsole(InteractiveInterpreter): | |
253 | """Closely emulate the behavior of the interactive Python interpreter. | |
254 | ||
255 | This class builds on InteractiveInterpreter and adds prompting | |
256 | using the familiar sys.ps1 and sys.ps2, and input buffering. | |
257 | ||
258 | """ | |
259 | ||
260 | def __init__(self, locals=None, filename="<console>"): | |
261 | """Constructor. | |
262 | ||
263 | The optional locals argument will be passed to the | |
264 | InteractiveInterpreter base class. | |
265 | ||
266 | The optional filename argument should specify the (file)name | |
267 | of the input stream; it will show up in tracebacks. | |
268 | ||
269 | """ | |
270 | InteractiveInterpreter.__init__(self, locals) | |
271 | self.filename = filename | |
272 | self.resetbuffer() | |
273 | ||
274 | def resetbuffer(self): | |
275 | """Reset the input buffer.""" | |
276 | self.buffer = [] | |
277 | ||
278 | def interact(self, banner=None, **parms): | |
279 | """Closely emulate the interactive Python console. | |
280 | ||
281 | The optional banner argument specify the banner to print | |
282 | before the first interaction; by default it prints a banner | |
283 | similar to the one printed by the real Python interpreter, | |
284 | followed by the current class name in parentheses (so as not | |
285 | to confuse this with the real interpreter -- since it's so | |
286 | close!). | |
287 | ||
288 | """ | |
289 | global optdir | |
290 | global rcCmd | |
291 | ||
292 | #sys.stderr.write("#DBX %s: enter InteractiveConsole.interact()\n" % (time.ctime())) | |
293 | ||
294 | # connect to command parser | |
295 | self.parser = parms['parser'] | |
296 | ||
297 | ## # if there are good_trap and/or bad_trap in symbol file, register | |
298 | ## # those as breakpoints | |
299 | ## # TODO by default, no breakpoint for good/bad_trap is set in sam, | |
300 | ## # we need an option to allow setting of those breakpoints. | |
301 | ## if parms['reposit'].symTable and not optdir.has_key('--blaze'): | |
302 | ## trapList = parms['reposit'].symTable.getTraps() | |
303 | ## for (trap,vaddr) in trapList: | |
304 | ## cmd = 'break %#x' % (vaddr) | |
305 | ## if self.parser.parseCmd(cmd, **parms): | |
306 | ## sys.stderr.write('ERROR: un-expected return value from parseCmd()\n') | |
307 | ||
308 | # if there are command(s) specified in config, execute those first | |
309 | _configRC = None | |
310 | if parms.has_key('confRC'): | |
311 | _configRC = parms['confRC'] | |
312 | ||
313 | tmp = '.rs_config_cmd' | |
314 | fdtmp = open(tmp, 'w') | |
315 | if _configRC: | |
316 | if _configRC.getObjData(ReadConfigDiag.TYPE_SYS_CONFIG, RC_SYS_CONFIG_OBJ, RC_COMMAND, silent=1): | |
317 | #sys.stderr.write("# %s: process config/init cmd\n" % (time.ctime())) | |
318 | for cmd in _configRC.getObjData(ReadConfigDiag.TYPE_SYS_CONFIG, RC_SYS_CONFIG_OBJ, RC_COMMAND): | |
319 | cmd = cmd.strip() | |
320 | # if get_addr() is part of the command, better resolve | |
321 | # that here, so we don't need to worry about that later | |
322 | # when execute the command | |
323 | index = cmd.find('get_addr') | |
324 | if index > -1: | |
325 | lop = cmd[:index] | |
326 | rop = cmd[index:] | |
327 | try: | |
328 | cmd = '%s%s' % (lop, eval(rop)) | |
329 | except: | |
330 | pass | |
331 | fdtmp.write('%s\n' % (cmd)) | |
332 | ||
333 | for cmd in rcCmd: | |
334 | fdtmp.write('%s\n' % (cmd)) | |
335 | ||
336 | fdtmp.close() | |
337 | # execute the commands specified in config file, if a command | |
338 | # cannot be run, just ignore it. | |
339 | self.parseExecfile('execfile("%s")' % (tmp), ignore=1, **parms) | |
340 | #sys.stderr.write("# %s: DBX: done config/init cmd\n" % (time.ctime())) | |
341 | ||
342 | # set prompt symbol | |
343 | if _configRC: | |
344 | prompt = _configRC.getObjData(ReadConfigDiag.TYPE_SYS_CONFIG, RC_SYS_CONFIG_OBJ, RC_PROMPT) | |
345 | else: | |
346 | prompt = 'sam' | |
347 | ||
348 | parms['reposit'].prompt = prompt | |
349 | ||
350 | try: | |
351 | sys.ps1 | |
352 | except AttributeError: | |
353 | sys.ps1 = "%s>>> " % (prompt) | |
354 | ||
355 | try: | |
356 | sys.ps2 | |
357 | except AttributeError: | |
358 | sys.ps2 = "%s... " % (prompt) | |
359 | ||
360 | try: | |
361 | sys.ps3 | |
362 | except AttributeError: | |
363 | sys.ps3 = "%s!!! " % (prompt) | |
364 | ||
365 | cprt = 'Type "copyright", "credits" or "license" for more information.' | |
366 | if banner is None: | |
367 | self.write("Python %s on %s\n%s\n(%s)\n" % | |
368 | (sys.version, sys.platform, cprt, | |
369 | self.__class__.__name__)) | |
370 | else: | |
371 | #self.write("# %s: %s\n" % (time.ctime(), str(banner))) | |
372 | pass | |
373 | ||
374 | more = 0 | |
375 | while 1: | |
376 | try: | |
377 | if more: | |
378 | prompt = sys.ps2 | |
379 | elif no_sam == 1: | |
380 | prompt = sys.ps1 | |
381 | elif (no_sam == 0) and sam.is_stopped(): | |
382 | prompt = sys.ps1 | |
383 | else: | |
384 | prompt = sys.ps3 | |
385 | ||
386 | try: | |
387 | line = self.raw_input(prompt) | |
388 | except EOFError: | |
389 | self.write("\n") | |
390 | break | |
391 | else: | |
392 | line = self.parser.parseCmd(line, **parms) | |
393 | if line != None: | |
394 | # most excepts are handled by push() and its inner | |
395 | # methods, so we won't see them here. | |
396 | if re.match(execfileRE, line): | |
397 | try: | |
398 | more = self.parseExecfile(line, **parms) | |
399 | except SystemExit: | |
400 | raise | |
401 | except: | |
402 | more = self.push(line) | |
403 | else: | |
404 | more = self.push(line) | |
405 | else: | |
406 | # to play it safe, flush out buffer when parser returns | |
407 | # None | |
408 | self.resetbuffer() | |
409 | more = 0 | |
410 | ||
411 | except KeyboardInterrupt: | |
412 | self.write("\nKeyboardInterrupt\n") | |
413 | self.resetbuffer() | |
414 | more = 0 | |
415 | ||
416 | def push(self, line): | |
417 | """Push a line to the interpreter. | |
418 | ||
419 | The line should not have a trailing newline; it may have | |
420 | internal newlines. The line is appended to a buffer and the | |
421 | interpreter's runsource() method is called with the | |
422 | concatenated contents of the buffer as source. If this | |
423 | indicates that the command was executed or invalid, the buffer | |
424 | is reset; otherwise, the command is incomplete, and the buffer | |
425 | is left as it was after the line was appended. The return | |
426 | value is 1 if more input is required, 0 if the line was dealt | |
427 | with in some way (this is the same as runsource()). | |
428 | ||
429 | """ | |
430 | self.buffer.append(line) | |
431 | source = "\n".join(self.buffer) | |
432 | more = self.runsource(source, self.filename) | |
433 | if not more: | |
434 | self.resetbuffer() | |
435 | return more | |
436 | ||
437 | def raw_input(self, prompt=""): | |
438 | """Write a prompt and read a line. | |
439 | ||
440 | The returned line does not include the trailing newline. | |
441 | When the user enters the EOF key sequence, EOFError is raised. | |
442 | ||
443 | The base implementation uses the built-in function | |
444 | raw_input(); a subclass may replace this with a different | |
445 | implementation. | |
446 | ||
447 | """ | |
448 | #return raw_input(prompt) | |
449 | line = raw_input(prompt) | |
450 | if re.match(NEW_CMD_RE, line): | |
451 | if line.startswith('@'): | |
452 | line = line[1:] | |
453 | newlist = [ line ] | |
454 | if not line.strip().endswith(')'): | |
455 | line = raw_input(prompt) | |
456 | while line.strip() and not line.strip().endswith(')'): | |
457 | newlist.append(line) | |
458 | line = raw_input(prompt) | |
459 | # remember to include the last line ending with ')' | |
460 | newlist.append(line) | |
461 | return ' '.join(newlist) | |
462 | else: | |
463 | return line | |
464 | ||
465 | ||
466 | def parseExecfile (self, cmdLine, ignore=0, **parms): | |
467 | """execfile() bypasses riesling cmd parser by default, so we have | |
468 | to intercept the execfile() command, and process its content line | |
469 | by line. | |
470 | """ | |
471 | global IGNORE_EXCEPT | |
472 | ||
473 | current_ignore_except = IGNORE_EXCEPT | |
474 | IGNORE_EXCEPT = ignore | |
475 | ||
476 | try: | |
477 | self._parseExecfile(cmdLine, ignore=ignore, **parms) | |
478 | except SystemExit: | |
479 | self.resetbuffer() | |
480 | raise | |
481 | except Exception, ex: | |
482 | self.resetbuffer() | |
483 | if ignore == 0: | |
484 | raise | |
485 | else: | |
486 | #sys.stderr.write('DBX: RiesCodeN2::parseExecfile: ignore <%s>, ex=%s\n' % (cmdLine, ex)) | |
487 | pass | |
488 | ||
489 | IGNORE_EXCEPT = current_ignore_except | |
490 | ||
491 | ||
492 | def _parseExecfile (self, cmdLine, ignore=0, **parms): | |
493 | """execfile() bypasses riesling cmd parser by default, so we have | |
494 | to intercept the execfile() command, and process its content line | |
495 | by line. | |
496 | """ | |
497 | global parser | |
498 | ||
499 | # the cmdLine is expected to be in the format of "execfile('zzz')" | |
500 | lindex = cmdLine.find('(') | |
501 | rindex = cmdLine.find(')') | |
502 | filename = eval(cmdLine[lindex+1:rindex].strip()) | |
503 | ||
504 | fin = open(filename) | |
505 | lineBuffer = [ ] | |
506 | for line in fin.readlines(): | |
507 | if line and line.startswith('def '): | |
508 | # a new function definition | |
509 | # CmdParserNi::registerDoc (self, key, fname, type, shortdoc, longdoc) | |
510 | defline = line.strip() | |
511 | ii = defline.rfind(':') | |
512 | if ii > -1: | |
513 | defline = defline[4:ii].strip() | |
514 | defkey = defline | |
515 | jj = defkey.find('(') | |
516 | if jj > -1: | |
517 | defkey = defkey[:jj].strip() | |
518 | parser.registerDoc(defkey, defline, '@DEF', filename+' : @def '+defline, None) | |
519 | # continue reading & concatenating line(s) until a complete | |
520 | # expression is formed. Remember to remove trailing newline | |
521 | if line[-1] == '\n': | |
522 | line = line[:-1] | |
523 | ||
524 | if (line != '') and (line[-1] == "\\"): | |
525 | # exclude the trailing "\" | |
526 | lineBuffer.append(line[:-1]) | |
527 | else: | |
528 | if len(lineBuffer) > 0: | |
529 | # make sure we include the last line of an expression | |
530 | lineBuffer.append(line) | |
531 | line = ' '.join(lineBuffer) | |
532 | lineBuffer = [ ] | |
533 | ||
534 | # keep track of leading whitespace | |
535 | i, n = 0, len(line) | |
536 | while (i < n) and (line[i] in string.whitespace): | |
537 | i = i + 1 | |
538 | indent = line[:i] | |
539 | cmd = line[i:] | |
540 | # if see nested execfile(), handle that recursively | |
541 | if re.match(execfileRE, cmd): | |
542 | try: | |
543 | self.parseExecfile(cmd, ignore=ignore, **parms) | |
544 | except Exception, ex: | |
545 | if ignore == 0: | |
546 | more = self.push(cmd) | |
547 | else: | |
548 | #sys.stderr.write('DBX: RiesCodeN2::_parseExecfile: ignore <%s>, ex=%s\n' % (cmd, ex)) | |
549 | self.resetbuffer() | |
550 | else: | |
551 | # if not an execfile(), parse the line, then add back the | |
552 | # leading whitespace | |
553 | try: | |
554 | cmd = self.parser.parseCmd(cmd, **parms) | |
555 | if cmd != None: | |
556 | # for run-command-file and run-python-file | |
557 | # commands, parser will return execfile(file-name), | |
558 | # since the file may contains frontend commands, | |
559 | # so we must pass them through parser line by line. | |
560 | if re.match(execfileRE, cmd): | |
561 | self.parseExecfile(cmd, ignore=ignore, **parms) | |
562 | else: | |
563 | newCmd = indent + cmd | |
564 | more = self.push(newCmd) | |
565 | else: | |
566 | self.resetbuffer() | |
567 | more = 0 | |
568 | ||
569 | except SystemExit: | |
570 | # if it is an exit signal, go with it. | |
571 | raise | |
572 | except Exception, ex: | |
573 | if ignore == 0: | |
574 | # report the error | |
575 | more = self.push(indent + 'raise') | |
576 | else: | |
577 | #sys.stderr.write('DBX: RiesCodeN2::_parseExecfile: ignore <%s>, ex2=%s\n' % (cmd, ex)) | |
578 | self.resetbuffer() | |
579 | ||
580 | fin.close() | |
581 | # send in an extra newline to close the last definition in the file. | |
582 | more = self.push("\n") | |
583 | ||
584 | ||
585 | def interact(banner=None, readfunc=None, local=None, **parms): | |
586 | """Closely emulate the interactive Python interpreter. | |
587 | ||
588 | This is a backwards compatible interface to the InteractiveConsole | |
589 | class. When readfunc is not specified, it attempts to import the | |
590 | readline module to enable GNU readline if it is available. | |
591 | ||
592 | Arguments (all optional, all default to None): | |
593 | ||
594 | banner -- passed to InteractiveConsole.interact() | |
595 | readfunc -- if not None, replaces InteractiveConsole.raw_input() | |
596 | local -- passed to InteractiveInterpreter.__init__() | |
597 | ||
598 | """ | |
599 | console = InteractiveConsole(local) | |
600 | if readfunc is not None: | |
601 | console.raw_input = readfunc | |
602 | else: | |
603 | try: | |
604 | import readline | |
605 | except: | |
606 | pass | |
607 | console.interact(banner, **parms) | |
608 | ||
609 | ||
610 | def usage(): | |
611 | """ | |
612 | """ | |
613 | ||
614 | print "usage: python %s [options]" % (sys.argv[0]) | |
615 | print "options:" | |
616 | print "\t -c conf-file # diag configuration file" | |
617 | print "\t -h # this usage information" | |
618 | #print "\t -s backend-config # riesling backend configuration file" | |
619 | print "\t -x conf-file # riesling configuration file" | |
620 | print "\t -p python-file # python configuration file to be executed at startup" | |
621 | print "\t --blaze system # type of system, n2" | |
622 | print "\t --blazeopt \"blaze options\" # blaze runtime options" | |
623 | print "\t --ar system # type of system, n2, ignore if --blaze is used" | |
624 | print "\t --rc riesling-rc # riesling config, a combination of -c & -x" | |
625 | ||
626 | sys.exit() | |
627 | ||
628 | ||
629 | def dbxTraceBack(): | |
630 | """ | |
631 | """ | |
632 | try: | |
633 | type, value, tb = sys.exc_info() | |
634 | sys.last_type = type | |
635 | sys.last_value = value | |
636 | sys.last_traceback = tb | |
637 | tblist = traceback.extract_tb(tb) | |
638 | del tblist[:1] | |
639 | list = traceback.format_list(tblist) | |
640 | if list: | |
641 | list.insert(0, "Traceback (most recent call last):\n") | |
642 | list[len(list):] = traceback.format_exception_only(type, value) | |
643 | finally: | |
644 | tblist = tb = None | |
645 | ||
646 | for line in list: | |
647 | sys.stderr.write('%s' % line) | |
648 | ||
649 | ||
650 | def createSystem (optdir): | |
651 | """invoke backend system constructor | |
652 | """ | |
653 | #sys.stderr.write("# %s: create riesling.sys\n" % (time.ctime())) | |
654 | ||
655 | global rsys | |
656 | global sim | |
657 | global ncpus | |
658 | global ncores | |
659 | global nucores | |
660 | global nstrands | |
661 | global nstrandObjs | |
662 | ||
663 | # if --blaze is specified, use that, otherwise use --ar | |
664 | if optdir.has_key('--blaze'): | |
665 | if optdir['--blaze'] == 'n2': | |
666 | optdir['--ar'] = 'n2' | |
667 | else: | |
668 | sys.stderr.write('ERROR: unknown system %s\n' % (optdir['--blaze'])) | |
669 | sys.exit() | |
670 | ||
671 | if optdir['--ar'] == 'n2': | |
672 | initN2() | |
673 | else: | |
674 | sys.stderr.write('ERROR: unknown system %s\n' % (optdir['--ar'])) | |
675 | sys.exit() | |
676 | ||
677 | if nucores == 0: | |
678 | nstrandObjs = ncpus * ncores * nstrands | |
679 | #sys.stderr.write('# %s: sys=%s=(cpu,core,strand)=(%d,%d,%d)\n' % (time.ctime(), optdir['--ar'].upper(), ncpus, ncores, nstrands)) | |
680 | else: | |
681 | nstrandObjs = ncpus * ncores * nucores * nstrands | |
682 | #sys.stderr.write('# %s: sys=(cpu,core,ucore,strand)=(%d,%d,%d,%d)\n' % (time.ctime(), ncpus, ncores, nucores, nstrands)) | |
683 | ||
684 | #sys.stderr.write("# %s: done creating riesling.sys\n" % (time.ctime())) | |
685 | ||
686 | ||
687 | def initN2 (): | |
688 | """init N2 structure/variables | |
689 | """ | |
690 | #sys.stderr.write("#DBX %s: initN2\n" % (time.ctime())) | |
691 | ||
692 | global rsys | |
693 | global sim | |
694 | ||
695 | sim = rsys | |
696 | initGenericN2(rsys) | |
697 | initN2_specific(rsys) | |
698 | ||
699 | ||
700 | def initN2_specific(rsys): | |
701 | """special setting for N2 | |
702 | """ | |
703 | #sys.stderr.write("#DBX %s: initN2_specific\n" % (time.ctime())) | |
704 | ||
705 | global optdir | |
706 | global configRC | |
707 | ||
708 | # cmp infomration in .riesling.rc | |
709 | ## OBJECT swvmem0 TYPE swerver-memory { | |
710 | ## debug_level : 0 | |
711 | ## ignore_sparc : ['1', '3', '4', '5', '6', '7'] | |
712 | ## irq : irq0 | |
713 | ## queue : th00 | |
714 | ## snoop : 0 | |
715 | ## thread_mask : 11xx11 | |
716 | ## thread_status : 0x9a00000000 | |
717 | ## threads : 110011 | |
718 | ## tso_checker : 0 | |
719 | ## } | |
720 | ||
721 | if configRC == None: | |
722 | return | |
723 | ||
724 | # (1) use 'cpu' to determine the number of cpus, (2) if thread_maskN is | |
725 | # available, use that to calculate CMP, (3) otherwise, use ignore_sparc | |
726 | # and threadsN to calculate CMP, less desirable. | |
727 | try: | |
728 | numCpus = int(configRC.getData('swvmem0', 'cpu', silent=1)) | |
729 | except: | |
730 | numCpus = 1 | |
731 | ||
732 | if ((optdir['--ar'] == 'n2') and ((numCpus < 0) or (numCpus > 1))): | |
733 | sys.stderr.write('ERROR: wrong number of %s cpus %d\n' % (optdir['--ar'].upper(), numCpus)) | |
734 | sys.exit(-1) | |
735 | ||
736 | for ii in range(numCpus): | |
737 | # set cmp init value, if any | |
738 | coreAvailable = 0x0L | |
739 | coreRunning = 0x0L | |
740 | coresAndStrands = configRC.getData('swvmem0', 'thread_mask'+str(ii), silent=1) | |
741 | if (not coresAndStrands) and (ii == 0): | |
742 | # for backward compatible, we used to just use thread_mask, | |
743 | # instead of thread_mask0 | |
744 | coresAndStrands = configRC.getData('swvmem0', 'thread_mask', silent=1) | |
745 | #sys.stderr.write('#DBX: use thread_mask%d=%s\n' % (ii, coresAndStrands)) | |
746 | if coresAndStrands: | |
747 | # if -sas_run_args=-DTHREAD_MASK is used, the value is stored in | |
748 | # coresAndStrands, which alone handles both cores & strands status. | |
749 | # this is the preferred option of specifying core/strand availability. | |
750 | # e.g., -sas_run_args=-DTHREAD_MASK=11xx11, every 2 digits (from right | |
751 | # to left) represents a core, 'xx' means disabled core. | |
752 | coresAndStrands = coresAndStrands.lower() | |
753 | tail = len(coresAndStrands) | |
754 | i = 0 | |
755 | firstCore = -1 | |
756 | while tail >= 2: | |
757 | core = coresAndStrands[tail-2:tail] | |
758 | if core.find('x') == -1: | |
759 | if firstCore == -1: | |
760 | # record the lowest core that is available | |
761 | firstCore = i | |
762 | coreAvailable = coreAvailable | (0xffL << i*8) | |
763 | coreRunning = coreRunning | (long(core, 16) << i*8) | |
764 | tail -= 2 | |
765 | i += 1 | |
766 | if tail > 0: | |
767 | # if the coresAndStrands string has odd digits, handle the last one | |
768 | core = coresAndStrands[:tail] | |
769 | if core.find('x') == -1: | |
770 | coreAvailable = coreAvailable | (0xffL << i*8) | |
771 | coreRunning = coreRunning | (long(core, 16) << i*8) | |
772 | # if no core is available, make the lowest one available | |
773 | if coreAvailable == 0x0: | |
774 | coreAvailable = 0xffL | |
775 | firstCore = 0 | |
776 | # if o strand is running, make the lowest strand of the lowest | |
777 | # available core running. | |
778 | if coreRunning == 0x0: | |
779 | coreRunning = 0x1L << (firstCore*8) | |
780 | #sys.stderr.write('#DBX: 1 coreAvailable=%#x\n' % (coreAvailable)) | |
781 | #sys.stderr.write('#DBX: 1 coreRunning=%#x\n' % (coreRunning)) | |
782 | else: | |
783 | # otherwise use the combination of cores & strands status | |
784 | parkedCoreList = configRC.getData('swvmem0', 'ignore_sparc', silent=1) | |
785 | if parkedCoreList != None: | |
786 | firstCore = -1 | |
787 | i = ii*8 | |
788 | while i < (ii*8 + 8): | |
789 | if not str(i) in parkedCoreList: | |
790 | if firstCore == -1: | |
791 | firstCore = i - (ii*8) | |
792 | coreAvailable = coreAvailable | (0xffL << (i-ii*8)*8) | |
793 | i += 1 | |
794 | if coreAvailable == 0x0: | |
795 | coreAvailable = 0xffL | |
796 | firstCore = 0 | |
797 | # check strands | |
798 | if ii == 0: | |
799 | # don't change existing variable | |
800 | unparkedStrands = configRC.getData('swvmem0', 'threads', silent=1) | |
801 | else: | |
802 | unparkedStrands = configRC.getData('swvmem0', 'threads'+str(ii), silent=1) | |
803 | if unparkedStrands != None: | |
804 | # only strands in enabled cores can be running | |
805 | coreRunning = long(unparkedStrands, 16) & coreAvailable | |
806 | if coreRunning == 0x0: | |
807 | coreRunning = 0x1L << (firstCore*8) | |
808 | else: | |
809 | # if no threads is specified, all strands in enabled cores | |
810 | # are enabled | |
811 | coreRunning = coreAvailable | |
812 | #sys.stderr.write('#DBX: 2 coreAvailable=%#x\n' % (coreAvailable)) | |
813 | #sys.stderr.write('#DBX: 2 coreRunning=%#x\n' % (coreRunning)) | |
814 | ||
815 | #sys.stderr.write('#DBX: 3 coreAvailable=%#x\n' % (coreAvailable)) | |
816 | #sys.stderr.write('#DBX: 3 coreRunning=%#x\n' % (coreRunning)) | |
817 | if coreAvailable != 0x0 and coreRunning != 0x0: | |
818 | headStrand = ii * 64 | |
819 | strands[headStrand].wrasi(0x41, 0x0L, coreAvailable) | |
820 | #???strands[headStrand].wrasi(0x41, 0x10L, coreAvailable) | |
821 | strands[headStrand].wrasi(0x41, 0x20L, coreAvailable) | |
822 | ||
823 | strands[headStrand].wrasi(0x41, 0x50L, coreRunning) | |
824 | #???strands[headStrand].wrasi(0x41, 0x58L, coreRunning) | |
825 | ||
826 | ||
827 | def initGenericNi (rsys): | |
828 | """init generic Niagara structure | |
829 | """ | |
830 | #sys.stderr.write("#DBX %s: initGenericNi\n" % (time.ctime())) | |
831 | ||
832 | global ncpus | |
833 | global ncores | |
834 | global nstrands | |
835 | global cpus | |
836 | global cores | |
837 | global strands | |
838 | ||
839 | #TODO need work | |
840 | if not rsys.__create_cpu__(1): | |
841 | print "Failed to create %d Ni cpu instance(s)" % 1 | |
842 | sys.exit(-1) | |
843 | ||
844 | # system configuration XXX | |
845 | ncpus = len(rsys.p) | |
846 | ncores = 8 | |
847 | nstrands = 4 | |
848 | ||
849 | for i in range(0,ncpus): | |
850 | cpus[i] = rsys.p[i] | |
851 | for j in range(0,8): | |
852 | cores[i*8 + j] = rsys.p[i].c[j] | |
853 | for k in range(0,4): | |
854 | strands[i*8*4 + j*8 + k] = rsys.p[i].c[j].s[k] | |
855 | ||
856 | ||
857 | def initGenericN2 (rsys): | |
858 | """init generic Niagara structure | |
859 | """ | |
860 | #sys.stderr.write("#DBX %s: initGenericN2\n" % (time.ctime())) | |
861 | ||
862 | global ncpus | |
863 | global ncores | |
864 | global nstrands | |
865 | global cpus | |
866 | global cores | |
867 | global strands | |
868 | ||
869 | # system configuration # | |
870 | ncpus = len(rsys.p) | |
871 | ncores = len(rsys.p0.c) | |
872 | nstrands = len(rsys.p0.c0.s) | |
873 | ||
874 | for i in range(0,ncpus): | |
875 | cpus[i] = rsys.p[i] | |
876 | for j in range(0,ncores): | |
877 | cores[i*ncores + j] = rsys.p[i].c[j] | |
878 | for k in range(0,nstrands): | |
879 | strands[i*ncores*nstrands + j*nstrands + k] = rsys.p[i].c[j].s[k] | |
880 | ||
881 | ||
882 | def readConfig (optdir): | |
883 | """read configuration data | |
884 | """ | |
885 | global NEW_CONFIG | |
886 | global configRC | |
887 | ||
888 | #sys.stderr.write("#DBX %s: read config\n" % (time.ctime())) | |
889 | configRies = None | |
890 | configDiag = None | |
891 | ||
892 | # read -x config (diag.simics) | |
893 | if optdir.has_key('-x'): | |
894 | configRies = ReadConfigRies.ReadConfigRies() | |
895 | configRies.readConfig(optdir['-x']) | |
896 | #sys.stderr.write('DBX: configRies = %s\n' % (configRies)) | |
897 | ||
898 | if configRies and configRies.data.has_key(ReadConfigRies.DIAG_CONF): | |
899 | # if DIAG_CONF is specified in -x, use that | |
900 | configDiag = ReadConfigDiag.ReadConfigDiag() | |
901 | configDiag.readConfig(configRies.data[ReadConfigRies.DIAG_CONF]) | |
902 | #sys.stderr.write('DBX: configDiag = %s\n' % (configDiag)) | |
903 | ||
904 | if configRies == None or configDiag == None: | |
905 | #sys.stderr.write('ERROR: no configuration data is given by either -c & -x, or --rc option\n') | |
906 | #usage() | |
907 | #sys.exit() | |
908 | return | |
909 | ||
910 | # system config | |
911 | try: | |
912 | newline = '@conf.%s.%s = %s' % (RC_SYS_CONFIG_OBJ, RC_PROMPT, configRies.data[ReadConfigRies.PROMPT]) | |
913 | except: | |
914 | # default | |
915 | newline = '@conf.%s.%s = ries' % (RC_SYS_CONFIG_OBJ, RC_PROMPT) | |
916 | configDiag.setDataLine(newline) | |
917 | ||
918 | try: | |
919 | newline = '@conf.%s.%s = %s' % (RC_SYS_CONFIG_OBJ, RC_MEM_IMAGE, RC_configRies.data[ReadConfigRies.MEM_IMAGE]) | |
920 | except: | |
921 | # default | |
922 | newline = '@conf.%s.%s = mem.image' % (RC_SYS_CONFIG_OBJ, RC_MEM_IMAGE) | |
923 | configDiag.setDataLine(newline) | |
924 | ||
925 | # process @def and def specified in config file | |
926 | handleAtDef(configRies, configDiag) | |
927 | # process @conf.zzz in configRies | |
928 | handleAtConf(configRies, configDiag) | |
929 | # process commands | |
930 | if configRies.data.has_key(ReadConfigRies.CMD): | |
931 | for cmd in configRies.data[ReadConfigRies.CMD]: | |
932 | newline = '@conf.%s.%s =+ %s' % (RC_SYS_CONFIG_OBJ, RC_COMMAND, cmd) | |
933 | configDiag.setDataLine(newline) | |
934 | # handle new_command() --- python command extension | |
935 | if configRies and configRies.data[ReadConfigRies.NEW_CMD] != {}: | |
936 | for cmdkey in configRies.data[ReadConfigRies.NEW_CMD].keys(): | |
937 | newline = '@conf.%s.%s =+ %s=%s' % (RC_SYS_CONFIG_OBJ, RC_COMMAND_EXT, cmdkey, configRies.data[ReadConfigRies.NEW_CMD][cmdkey]) | |
938 | configDiag.setDataLine(newline) | |
939 | ||
940 | fdtmp = open(NEW_CONFIG, 'w') | |
941 | fdtmp.write('%s\n' % (configDiag)) | |
942 | fdtmp.close() | |
943 | ||
944 | # create the rc object to be used from now on | |
945 | configRC = ReadConfigDiag.ReadConfigDiag() | |
946 | configRC.readConfig(NEW_CONFIG) | |
947 | ||
948 | ||
949 | def constructSystem (optdir): | |
950 | """construct a backend system | |
951 | """ | |
952 | global rsys | |
953 | global sysAddr | |
954 | ||
955 | #sys.stderr.write("#DBX %s: construct system\n" % (time.ctime())) | |
956 | ||
957 | # create backend system and get the sys object's address, to be passed | |
958 | # to nasAPI or blaze. | |
959 | try: | |
960 | createSystem(optdir) | |
961 | except: | |
962 | dbxTraceBack() | |
963 | ||
964 | #sys.stderr.write('# %s: sys addr=%u\n' % (time.ctime(), sysAddr)) | |
965 | ||
966 | ||
967 | #def handleAtDef (globals): | |
968 | def handleAtDef (configRies, configDiag): | |
969 | """ | |
970 | """ | |
971 | global ATDEF | |
972 | ||
973 | #sys.stderr.write("#DBX %s: process @def/def\n" % (time.ctime())) | |
974 | ||
975 | try: | |
976 | # remove old file, if any | |
977 | os.remove(ATDEF) | |
978 | except: | |
979 | pass | |
980 | if configRies: | |
981 | tfd = open(ATDEF, 'w') | |
982 | if configRies.data[ReadConfigRies.AT_DEF] != {}: | |
983 | for func in configRies.data[ReadConfigRies.AT_DEF].keys(): | |
984 | tfd.write('%s\n' % (''.join(configRies.data[ReadConfigRies.AT_DEF][func]))) | |
985 | tfd.write('\n') | |
986 | ||
987 | tfd.close() | |
988 | ||
989 | if configDiag: | |
990 | newline = '@conf.%s.%s = %s' % (RC_SYS_CONFIG_OBJ, RC_FUNC_DEF, ATDEF) | |
991 | configDiag.setDataLine(newline) | |
992 | ||
993 | # run the function definition in ATDEF | |
994 | if os.path.getsize(ATDEF) > 0: | |
995 | execfile(ATDEF, globals()) | |
996 | ||
997 | ||
998 | def handleAtConf (configRies, configDiag): | |
999 | """ | |
1000 | """ | |
1001 | #sys.stderr.write("#DBX %s: process @conf.zzz\n" % (time.ctime())) | |
1002 | ||
1003 | if configDiag and configRies and configRies.data[ReadConfigRies.AT_CONF] != []: | |
1004 | for dline in configRies.data[ReadConfigRies.AT_CONF]: | |
1005 | j = dline.find('=+') | |
1006 | if j > -1: | |
1007 | # the value is to be appended to existing one(s) | |
1008 | lop = dline[:j].strip() | |
1009 | rop = dline[j+2:].strip() | |
1010 | if re.match(DIGITAL_RE, rop): | |
1011 | # if rhs are all [0-9], leave as is | |
1012 | newline = dline | |
1013 | else: | |
1014 | try: | |
1015 | # we need to eval things like get_addr() | |
1016 | newline = '%s =+ %#x' % (lop, eval(rop)) | |
1017 | except: | |
1018 | # if the right operand cannot be eval'ed, just use its | |
1019 | # original form. | |
1020 | newline = dline | |
1021 | else: | |
1022 | # the value is to be used to overwrite existing one | |
1023 | j = dline.find('=') | |
1024 | lop = dline[:j].strip() | |
1025 | rop = dline[j+1:].strip() | |
1026 | if re.match(DIGITAL_RE, rop): | |
1027 | # if rhs are all [0-9], leave as is | |
1028 | newline = dline | |
1029 | else: | |
1030 | try: | |
1031 | newline = '%s = %#x' % (lop, eval(rop)) | |
1032 | except: | |
1033 | newline = dline | |
1034 | # add/update value in configDiag | |
1035 | configDiag.setDataLine(newline) | |
1036 | ||
1037 | ||
1038 | def createCommandParser (): | |
1039 | """ | |
1040 | """ | |
1041 | global parser | |
1042 | global riesReposit | |
1043 | global nstrandObjs | |
1044 | global configRC | |
1045 | ||
1046 | #sys.stderr.write("#DBX %s: createCommandParser()\n" % (time.ctime())) | |
1047 | ||
1048 | parser = CmdParserNi.CmdParserNi(riesReposit) | |
1049 | # set the value in parser for later use | |
1050 | parser.setNstrandObjs(nstrandObjs) | |
1051 | ||
1052 | # init zzz_cmd_RS() & RS_zzz() functions in CmdParserNiCmd | |
1053 | #sys.stderr.write("#DBX %s: create CmdParserNiCmd\n" % (time.ctime())) | |
1054 | initCmdParserNiCmd(riesReposit, nstrandObjs) | |
1055 | ||
1056 | # handle new_command() --- python command extension | |
1057 | if configRC: | |
1058 | if configRC.getObjData(ReadConfigDiag.TYPE_SYS_CONFIG, RC_SYS_CONFIG_OBJ, RC_COMMAND_EXT, silent=1): | |
1059 | for cmd in configRC.getObjData(ReadConfigDiag.TYPE_SYS_CONFIG, RC_SYS_CONFIG_OBJ, RC_COMMAND_EXT): | |
1060 | i = cmd.find('=') | |
1061 | cmdkey = cmd[:i].strip() | |
1062 | cmdVal = cmd[i+1].strip() | |
1063 | parser.registerCommand(cmdkey, cmdVal) | |
1064 | ||
1065 | ||
1066 | def registerAtDef (): | |
1067 | """ | |
1068 | """ | |
1069 | global ATDEF | |
1070 | global parser | |
1071 | ||
1072 | #sys.stderr.write("#DBX %s: registerAtDef()\n" % (time.ctime())) | |
1073 | ||
1074 | try: | |
1075 | if os.path.getsize(ATDEF) > 0: | |
1076 | fdtmp = open(ATDEF, 'r') | |
1077 | for line in fdtmp.readlines(): | |
1078 | if line.startswith('def '): | |
1079 | # def func(...): | |
1080 | lindex = line.find(' ') | |
1081 | mindex = line.find('(', lindex) | |
1082 | rindex = line.rfind(':') | |
1083 | fname = line[lindex+1:mindex].strip() | |
1084 | func = line[lindex+1:rindex].strip() | |
1085 | parser.registerDoc(fname, func, '@DEF', '@def '+func, None) | |
1086 | except: | |
1087 | pass | |
1088 | ||
1089 | ||
1090 | def debugMomConfig (config): | |
1091 | """this function is N2 specific | |
1092 | """ | |
1093 | momConfig = ReadConfigDiag.ReadConfigDiag() | |
1094 | momConfig.readConfig(config) | |
1095 | momKeys = momConfig.getObjKeys('mom', 'mom0', silent=1) | |
1096 | if momKeys != []: | |
1097 | klist = momKeys | |
1098 | klist.sort() | |
1099 | for key in klist: | |
1100 | data = momConfig.getData('mom0', key, silent=1) | |
1101 | sys.stderr.write('MOM: %s : %s\n' % (key, data)) | |
1102 | ||
1103 | def blazercCmd (newBlazeCmd): | |
1104 | """extract riesling commands (the ones with #@) from blazerc | |
1105 | """ | |
1106 | global riesReposit | |
1107 | global optdir | |
1108 | ||
1109 | # handle blaze -c option, if one is specifed. | |
1110 | if not optdir.has_key('--blazeopt'): | |
1111 | return [] | |
1112 | else: | |
1113 | blazeopt = optdir['--blazeopt'] | |
1114 | ||
1115 | #sys.stderr.write('#DBX: SamFR::blazercCmd: blazeopt=%s\n' % (blazeopt)) #DBX | |
1116 | ||
1117 | ii = blazeopt.find('-c ') | |
1118 | if ii < 0: | |
1119 | # no -c option is specified | |
1120 | return [] | |
1121 | ||
1122 | tmp = blazeopt[ii+3:].strip() | |
1123 | jj = tmp.find(' ') | |
1124 | if jj == -1: | |
1125 | rcname = tmp | |
1126 | else: | |
1127 | rcname = tmp[:jj] | |
1128 | try: | |
1129 | fdrc = open(rcname, 'r') | |
1130 | except: | |
1131 | sys.stderr.write('ERROR: fail to open %s\n' % (rcname)) | |
1132 | sys.exit() | |
1133 | ||
1134 | # sample sysconf statements in blazerc | |
1135 | # sysconf cpu name=cpu0 cpu-type=SUNW,UltraSPARC-T1 clock-frequency=0x200000 system-frequency=0x100000 cpu-loopticks=2 cpu-stickincr=1 | |
1136 | # sysconf dumbserial serial1 type=GUEST startpa=0x9f10000000 endpa=0x9f1000004f | |
1137 | # sysconf dumbserial serial2 type=HYPERVISOR startpa=0xfff0c2c000 endpa=0xfff0c2c04f | |
1138 | ||
1139 | cmd = [ ] | |
1140 | cpuCount = 0 | |
1141 | riesReposit.blazeNumcpu = 0 | |
1142 | for line in fdrc.readlines(): | |
1143 | line = line.strip() | |
1144 | if line and line.startswith('#@'): | |
1145 | cmd.append(line[2:]) | |
1146 | elif line and line.startswith('conf numcpu'): | |
1147 | # record the numcpu value in blazerc and initialize the pmask value | |
1148 | # base on that. | |
1149 | tokens = line.split() | |
1150 | riesReposit.blazeNumcpu = int(tokens[2]) | |
1151 | riesReposit.pmask = 1L | |
1152 | for i in range(1,riesReposit.blazeNumcpu): | |
1153 | riesReposit.pmask = (riesReposit.pmask << 1) | 0x1 | |
1154 | elif line.startswith('sysconf'): | |
1155 | # sysconf -p ../../lib | |
1156 | # sysconf cpu name=cpu0 cpu-type=... | |
1157 | # sysconf dumbserial serial1 type=GUEST ... | |
1158 | tokens = line.split() | |
1159 | if (tokens[1].lower() != 'cpu') and (tokens[1] != '-p'): | |
1160 | newBlazeCmd.append(tokens[1]) | |
1161 | newBlazeCmd.append(tokens[2]) | |
1162 | elif (tokens[1].lower() == 'cpu') and (tokens[2].lower().startswith('name=cpu')): | |
1163 | cpuCount += 1 | |
1164 | ||
1165 | if (riesReposit.blazeNumcpu == 0) and (cpuCount > 0): | |
1166 | riesReposit.blazeNumcpu = cpuCount | |
1167 | riesReposit.pmask = 0x1L | |
1168 | for i in range(1,riesReposit.blazeNumcpu): | |
1169 | riesReposit.pmask = (riesReposit.pmask << 1) | 0x1L | |
1170 | ||
1171 | if optdir['--blaze'] in ['n2']: | |
1172 | # convert blaze numcpu to coreAvailable | |
1173 | if (riesReposit.blazeNumcpu % riesReposit.nstrands) == 0: | |
1174 | cores = riesReposit.blazeNumcpu / riesReposit.nstrands | |
1175 | else: | |
1176 | cores = (riesReposit.blazeNumcpu / riesReposit.nstrands) + 1 | |
1177 | ||
1178 | for i in range(0, cores): | |
1179 | if (i % riesReposit.ncores) == 0: | |
1180 | setcmp = 0 | |
1181 | cpuid = i / riesReposit.ncores | |
1182 | coreAvail = 0xffL | |
1183 | else: | |
1184 | setcmp = 0 | |
1185 | #coreAvail = (coreAvail << ((i%riesReposit.ncores)*8)) | 0xffL | |
1186 | coreAvail = (coreAvail << 8) | 0xffL | |
1187 | if ((i+1) % riesReposit.ncores) == 0: | |
1188 | # re-adjust CMP registers | |
1189 | setcmp = 1 | |
1190 | strandid = cpuid * riesReposit.ncores * riesReposit.nstrands | |
1191 | coreAvailable = strands[strandid].rdasi(0x41, 0x0L) & coreAvail | |
1192 | strands[strandid].wrasi(0x41, 0x0L, coreAvailable) | |
1193 | #???strands[strandid].wrasi(0x41, 0x10L, coreAvailable) | |
1194 | strands[strandid].wrasi(0x41, 0x20L, coreAvailable) | |
1195 | coreRunning = strands[strandid].rdasi(0x41, 0x50L) & coreAvail | |
1196 | strands[strandid].wrasi(0x41, 0x50L, coreRunning) | |
1197 | #???strands[strandid].wrasi(0x41, 0x58L, coreRunning) | |
1198 | ||
1199 | # final set | |
1200 | if setcmp == 0: | |
1201 | strandid = cpuid * riesReposit.ncores * riesReposit.nstrands | |
1202 | coreAvailable = strands[strandid].rdasi(0x41, 0x0L) & coreAvail | |
1203 | strands[strandid].wrasi(0x41, 0x0L, coreAvailable) | |
1204 | #???strands[strandid].wrasi(0x41, 0x10L, coreAvailable) | |
1205 | strands[strandid].wrasi(0x41, 0x20L, coreAvailable) | |
1206 | coreRunning = strands[strandid].rdasi(0x41, 0x50L) & coreAvail | |
1207 | strands[strandid].wrasi(0x41, 0x50L, coreRunning) | |
1208 | #???strands[strandid].wrasi(0x41, 0x58L, coreRunning) | |
1209 | ||
1210 | if optdir.has_key('--pmask'): | |
1211 | mask = long(optdir['--pmask'],16) | |
1212 | coreRunning = mask & strands[strandid].rdasi(0x41, 0x0L) | |
1213 | strands[strandid].wrasi(0x41, 0x50L, coreRunning) | |
1214 | ||
1215 | return cmd | |
1216 | ||
1217 | ||
1218 | ######################## | |
1219 | ##### MAIN ##### | |
1220 | ######################## | |
1221 | #if __name__ == '__main__': | |
1222 | ||
1223 | from CmdParserNiCmd import * | |
1224 | import CmdParserNi | |
1225 | import ReadConfigRies | |
1226 | import ReadConfigDiag | |
1227 | from SamUtility import * | |
1228 | ||
1229 | rsys = None | |
1230 | # default arch configuration | |
1231 | (ncpus, ncores, nucores, nstrands) = (0,0,0,0) | |
1232 | (cpus, cores, ucores, strands) = ({},{},{},{}) | |
1233 | configRC = None | |
1234 | riesReposit = None | |
1235 | optdir = None | |
1236 | parser = None | |
1237 | # folder to keep UI commands (the ones start with #@) extracted from blazerc | |
1238 | rcCmd = [ ] | |
1239 | nstrandObjs = 0 | |
1240 | ATDEF = '.riesling.def' | |
1241 | NEW_CONFIG = '.riesling.rc' | |
1242 | ||
1243 | ||
1244 | def init (pfeSim, _optdir, uppers={}): | |
1245 | """ | |
1246 | frontend parser startup | |
1247 | """ | |
1248 | global rsys | |
1249 | global ncpus | |
1250 | global ncores | |
1251 | global nstrands | |
1252 | global cpus | |
1253 | global cores | |
1254 | global strands | |
1255 | global configRC | |
1256 | global riesReposit | |
1257 | global optdir | |
1258 | global parser | |
1259 | global rcCmd | |
1260 | ||
1261 | # initialize SamUtility | |
1262 | initSamUtility(pfeSim) | |
1263 | ||
1264 | optdir = _optdir | |
1265 | ||
1266 | import commands | |
1267 | #sys.stderr.write("# %s\n" % (commands.getoutput('uname -a'))) | |
1268 | #sys.stderr.write("# Python %s\n" % sys.version) | |
1269 | #sys.stderr.write("# %s: start sam frontend\n" % (time.ctime())) | |
1270 | ||
1271 | # read -x diag.sam (and then diag.conf) | |
1272 | if optdir.has_key('--rc'): | |
1273 | # if --rc is used, then ignore -x | |
1274 | # NOTE it will be tricky to use --rc in rtl cosim environment, | |
1275 | # as we need socket-id to connect with rtl, and the id will | |
1276 | # change from run to run, which is recorded in diag.conf by sims, | |
1277 | # the socket-id in --rc won't work as it was from the previous | |
1278 | # run, if at all. | |
1279 | #sys.stderr.write('WARNING: cannot use --rc %s when cosim with RTL\n' % (optdir['--rc'])) | |
1280 | NEW_CONFIG = optdir['--rc'] | |
1281 | configRC = ReadConfigDiag.ReadConfigDiag() | |
1282 | configRC.readConfig(optdir['--rc']) | |
1283 | # run function definition specified by -x or --rc | |
1284 | ATDEF = configRC.getData(RC_SYS_CONFIG_OBJ, RC_FUNC_DEF) | |
1285 | #sys.stderr.write('#DBX: func-def %s\n' % (funcDef)) | |
1286 | if ATDEF and os.path.getsize(ATDEF) > 0: | |
1287 | execfile(ATDEF, globals()) | |
1288 | else: | |
1289 | readConfig(optdir) | |
1290 | ||
1291 | # check if blaze/mom will be used | |
1292 | if configRC: | |
1293 | momObjs = configRC.getObjIds('mom', silent=1) | |
1294 | if (momObjs != []): | |
1295 | # if user specifies a special version of mom, use that instead | |
1296 | # of the default file. | |
1297 | try: | |
1298 | momSoPath = configRC.data['mom']['mom0']['so_path'] | |
1299 | except: | |
1300 | momSoPath = None | |
1301 | if (momSoPath): | |
1302 | if (os.path.exists(momSoPath)): | |
1303 | cmd='ls -l *mom* ; /bin/cp -p %s . ; ls -l *mom*' % (momSoPath) | |
1304 | os.system(cmd) | |
1305 | else: | |
1306 | sys.stderr.write('ERROR: mom module %s does not exist\n' % (momSoPath)) | |
1307 | sys.exit() | |
1308 | ||
1309 | if (momObjs != []) and (not optdir.has_key('--blaze')): | |
1310 | prompt = configRC.data[ReadConfigDiag.TYPE_SYS_CONFIG][RC_SYS_CONFIG_OBJ][RC_PROMPT] | |
1311 | if prompt == 'nas': | |
1312 | optdir['--blaze'] = 'n2' | |
1313 | else: | |
1314 | sys.stderr.write('ERROR: unknown system prompt %s\n' % (prompt)) | |
1315 | sys.exit() | |
1316 | ||
1317 | ||
1318 | # construct backend system | |
1319 | SYSTEM = 'rsys' | |
1320 | rsys = pfeSim | |
1321 | sysAddr = 0L | |
1322 | constructSystem(optdir) | |
1323 | ||
1324 | if configRC: | |
1325 | MEM_IMAGE = configRC.getObjData(ReadConfigDiag.TYPE_SYS_CONFIG, RC_SYS_CONFIG_OBJ, RC_MEM_IMAGE) | |
1326 | else: | |
1327 | MEM_IMAGE = 'mem.image' | |
1328 | ||
1329 | # a Repository object for variables that should be availabe globally, | |
1330 | # this is most useful in interactive mode, where we may need to eval() | |
1331 | # commands at various locations. | |
1332 | import Repository | |
1333 | riesReposit = Repository.Repository() | |
1334 | ||
1335 | riesReposit.riesling = rsys | |
1336 | riesReposit.topName = SYSTEM | |
1337 | # record main program's globals so other functions deep in the code | |
1338 | # structure can have access to top layer values. | |
1339 | riesReposit.globals = globals() | |
1340 | riesReposit.sysAddr = sysAddr | |
1341 | riesReposit.ncpus = ncpus | |
1342 | riesReposit.ncores = ncores | |
1343 | riesReposit.nucores = nucores | |
1344 | riesReposit.nstrands = nstrands | |
1345 | riesReposit.optdir = optdir | |
1346 | riesReposit.cpus = cpus | |
1347 | riesReposit.cores = cores | |
1348 | riesReposit.ucores = ucores | |
1349 | riesReposit.strands = strands | |
1350 | ||
1351 | #declare variables for single, double and quad fp regs | |
1352 | # is it ok to put n2 specific constants here? ---> fp is generic enough | |
1353 | riesReposit.nSpregs = 64 | |
1354 | riesReposit.nDpregs = 32 | |
1355 | riesReposit.nQpregs = 16 | |
1356 | riesReposit.arch = optdir['--ar'] | |
1357 | ||
1358 | # create a command parser | |
1359 | #import CmdParserNi | |
1360 | # we have to import CmdParserNiCmd* at this level so that all the | |
1361 | # RS_zzz() can be globally available | |
1362 | createCommandParser() | |
1363 | ||
1364 | parms = { 'confRC':configRC, 'reposit':riesReposit, 'parser':parser } | |
1365 | ||
1366 | # register @def/def functions' doc, if any | |
1367 | registerAtDef() | |
1368 | ||
1369 | # load personal customization, if available | |
1370 | filename = os.environ.get('PYTHONSTARTUP') | |
1371 | if filename and os.path.isfile(filename): | |
1372 | #sys.stderr.write('# Executing User Startup %s .. \n' % filename) | |
1373 | execfile(filename) | |
1374 | ||
1375 | #sys.stderr.write("#DBX %s: ready for interactive mode\n" % (time.ctime())) | |
1376 | ||
1377 | if optdir.has_key('--blaze'): | |
1378 | # parse blaze options, if available | |
1379 | newBlazeCmd = [ ] | |
1380 | rcCmd = blazercCmd(newBlazeCmd) | |
1381 | #sys.stderr.write('#DBX: rcCmd=%s\n' % (rcCmd)) #DBX | |
1382 | # register blaze cmooand map | |
1383 | import SamCmdMap | |
1384 | samCmdMap = SamCmdMap.SamCmdMap() | |
1385 | parser.registerCmdMap(samCmdMap) | |
1386 | # register new commands introduced by sysconf in blazerc | |
1387 | for cmd in newBlazeCmd: | |
1388 | samCmdMap.cmdMap.append(cmd) | |
1389 | # switch to use 'sam' as prompt symbol, sam is blaze | |
1390 | # infrastructure plus risling core | |
1391 | if configRC: | |
1392 | newline = '@conf.%s.%s = sam' % (RC_SYS_CONFIG_OBJ, RC_PROMPT) | |
1393 | configRC.setDataLine(newline) | |
1394 | ||
1395 | # Historically a -p introduced pythoon script, so for now keep checking | |
1396 | # for this -p option | |
1397 | ||
1398 | if optdir.has_key('-p'): | |
1399 | files = optdir['-p'].split(',') | |
1400 | for file in files: | |
1401 | execfile(file) | |
1402 | ||
1403 | # The modern way is to treat all command line arguments that have no | |
1404 | # dash something prefix as python scripts that are executed in the | |
1405 | # order given | |
1406 | ||
1407 | if optdir.has_key('scripts'): | |
1408 | for script in optdir['scripts']: | |
1409 | execfile(script) | |
1410 | ||
1411 | # enter interactive mode | |
1412 | #interact(banner="Sam Frontend Reader", local=vars(), **parms) | |
1413 | ||
1414 | # merge (optional) upper level globals() before go into the customized | |
1415 | # interpreter | |
1416 | uppers.update(globals()) | |
1417 | uppers.update(locals()) | |
1418 | interact(banner="Sam Frontend Reader", local=uppers, **parms) |