Changeset 2087 for src/ASM/CPP2012-asm
- Timestamp:
- Jun 15, 2012, 11:40:25 AM (9 years ago)
- Location:
- src/ASM/CPP2012-asm
- Files:
-
- 2 edited
Legend:
- Unmodified
- Added
- Removed
-
src/ASM/CPP2012-asm/cpp-2012-asm.bib
r2083 r2087 25 25 author = {Gerwin Klein and Tobias Nipkow}, 26 26 title = {A machine-checked model for a {Java-like} language, virtual machine and compiler}, 27 journal = {{ TOPLAS}},27 journal = {{ACM} Transactions on Programming Languages and Systems}, 28 28 volume = {28}, 29 29 number = {4}, … … 36 36 author = {Gerwin Klein and June Andronick and Kevin Elphinstone and Gernot Heiser and David Cock and Philip Derrin and Dhammika Elkaduwe and Kai Engelhardt and Rafal Kolanski and Michael Norrish and Thomas Sewell, Harvey Tuch and Simon Winwood}, 37 37 title = {{seL4}: Formal verification of an operating system kernel}, 38 journal = { {CACM}},38 journal = {Communications of the {ACM}}, 39 39 issue = {6}, 40 40 volume = {53}, … … 47 47 author = {Xavier Leroy}, 48 48 title = {Formal verification of a realistic compiler}, 49 journal = { {CACM}},49 journal = {Communications of the {ACM}}, 50 50 volume = {52}, 51 51 number = {7}, … … 80 80 author = {Robert Atkey}, 81 81 title = {{CoqJVM}: An executable specification of the {Java Virtual Machine} using dependent types}, 82 booktitle = { {TYPES}},82 booktitle = {Types for Proofs and Programs}, 83 83 pages = {18--32}, 84 84 year = {2007} … … 98 98 author = {Jaap Boender and Claudio {Sacerdoti Coen}}, 99 99 title = {On the correctness of a branch displacement algorithm}, 100 booktitle = { {CPP}},100 booktitle = {Certified Proofs and Programs {(CPP)}}, 101 101 year = {2012}, 102 102 note = {Submitted} … … 116 116 author = {Harvey Tuch and Gerwin Klein and Michael Norrish}, 117 117 title = {Types, Bytes, and Separation Logic}, 118 booktitle = { {POPL}},118 booktitle = {Principles of Programming Languages {(POPL)}}, 119 119 pages = {97--108}, 120 120 year = {2007} … … 126 126 author = {Gerwin Klein and June Andronick and Kevin Elphinstone and Gernot Heiser and David Cock and Philip Derrin and Dhammika Elkaduwe and Kai Engelhardt and Rafal Kolanski and Michael Norrish and Thomas Sewell, Harvey Tuch and Simon Winwood}, 127 127 title = {{seL4}: Formal verification of an operating system kernel}, 128 booktitle = {{ SOSP}},128 booktitle = {{ACM} Symposium on Operating Systems Principles {(SOSP)}}, 129 129 year = {2009} 130 130 } … … 152 152 author = {Matthieu Sozeau}, 153 153 title = {Subset Coercions in {Coq}}, 154 booktitle = { {TYPES}},154 booktitle = {Types for proofs and programs}, 155 155 pages = {237--252}, 156 156 year = {2006} -
src/ASM/CPP2012-asm/cpp-2012-asm.tex
r2083 r2087 37 37 \title{On the correctness of an optimising assembler for the Intel MCS-51 microprocessor\thanks{The project CerCo acknowledges the financial support of the Future and Emerging Technologies (FET) programme within the Seventh Framework Programme for Research of the European Commission, under FET-Open grant number: 243881}} 38 38 \author{Dominic P. Mulligan \and Claudio Sacerdoti Coen} 39 \institute{Dipartimento di Scienze dell'Informazione, Universit\'a di Bologna}39 \institute{Dipartimento di Scienze dell'Informazione,\\ Universit\'a di Bologna} 40 40 41 41 \bibliographystyle{splncs03} … … 56 56 Assembly language programs can manipulate concrete addresses in arbitrary ways. 57 57 Our proof strategy contains a tracking facility for `good addresses' and only programs that use good addresses have their semantics preserved under assembly. 58 Our strategy offers increased flexibility over the traditional approach of keeping addresses in assembly opaque.58 Our strategy offers increased flexibility over the traditional approach to proving the correctness of assemblers, wherein addresses in assembly are kept opaque and immutable. 59 59 In particular, we may experiment with allowing the benign manipulation of addresses. 60 60 \end{abstract} … … 77 77 As a result our C compiler, to have any sort of hope of successfully compiling realistic programs for embedded devices, ought to produce `tight' machine code. 78 78 79 In order to do this, we must solve the `branch displacement' problem---deciding how best to expand jumps to labels in assembly language to machine code jumps.80 Clearly a correct but efficient strategy would be to expand all unconditional jumps to the MCS-51's \texttt{LJMP} instruction, and all conditional jumps to a set configuration of jumps using \texttt{LJMP} instructions; this is inefficient.81 Finding an efficient solution with this expansion process is not trivial, and is a well-known problem for those writing assemblers targetting RISC architectures .82 83 Branch displacement is not a simple problem to solve and requires the implementation of an optimising assembler.79 In order to do this, we must solve the `branch displacement' problem---deciding how best to expand pseudojumps to labels in assembly language to machine code jumps. 80 Clearly a correct but efficient strategy would be to expand all unconditional pseudojumps to the MCS-51's \texttt{LJMP} instruction, and all conditional pseudojumps to a set configuration of jumps using \texttt{LJMP} instructions; this is inefficient and a waste of valuable code memory space. 81 Finding an efficient solution with this expansion process is not trivial, and is a well-known problem for those writing assemblers targetting RISC architectures (for instance, see~\cite{holmes:branch:2006}). 82 83 To free the CerCo C compiler from having to consider complications relating to branch displacement, we have chosen to implement an optimising assembler, whose input language the compiler will target. 84 84 Labels, conditional jumps to labels, a program preamble containing global data and a \texttt{MOV} instruction for moving this global data into the MCS-51's one 16-bit register all feature in our assembly language. 85 We simplify the proof by assuming that all our assembly programs are pre-linked (i.e. we do not formalise a linker ).85 We simplify the proof by assuming that all our assembly programs are pre-linked (i.e. we do not formalise a linker---this is left for future work). 86 86 87 87 Further, we must make sure that the assembly process does not change the timing characteristics of an assembly program for two reasons. … … 105 105 This is achieved by means of dependent types: the assembly function is total over a program, a policy and the proof that the policy is correct for that program. 106 106 107 Policies do not exist in only a limited number of circumstances: namely, if a pseudoinstruction attempts to jump to a label that does not exist, or the program is too large to fit in code memory, even after shrinking jumps according to the bestpolicy.107 Policies do not exist in only a limited number of circumstances: namely, if a pseudoinstruction attempts to jump to a label that does not exist, or the program is too large to fit in code memory, even after shrinking jumps according to the policy. 108 108 The first circumstance is an example of a serious compiler error, as an ill-formed assembly program was generated, and does not (and should not) count as a mark against the completeness of the assembler. 109 109 … … 155 155 Our emulator centres around a \texttt{Status} record, describing the microprocessor's state. 156 156 This record contains fields corresponding to the microprocessor's program counter, registers, and so on. 157 At the machine code level, code memory is implemented as a compact trie of bytes ,addressed by the program counter.157 At the machine code level, code memory is implemented as a compact trie of bytes addressed by the program counter. 158 158 We parameterise \texttt{Status} records by this representation as a few technical tasks manipulating statuses are made simpler using this approach, as well as permitting a modicum of abstraction. 159 159 … … 163 163 \end{lstlisting} 164 164 %The function \texttt{execute} allows one to execute an arbitrary, but fixed (due to Matita's normalisation requirement) number of steps of a program. 165 The function \texttt{execute\_1} closely matches the operation of the MCS-51 hardware, as described by a Siemen's manufacturer's data sheet .165 The function \texttt{execute\_1} closely matches the operation of the MCS-51 hardware, as described by a Siemen's manufacturer's data sheet (specifically, this one~\cite{siemens:2011}). 166 166 We first fetch, using the program counter, from code memory the first byte of the instruction to be executed, decoding the resulting opcode, fetching more bytes as is necessary. 167 Decoded instructions are represented as an inductive type, where $\llbracket - \rrbracket$ denotes a vector:167 Decoded instructions are represented as an inductive type, where $\llbracket - \rrbracket$ denotes a fixed-length vector: 168 168 \begin{lstlisting} 169 169 inductive preinstruction (A: Type[0]): Type[0] := … … 183 183 184 184 Once decoded, execution proceeds by a case analysis on the decoded instruction, following the operation of the hardware. 185 For example :185 For example, the \texttt{DEC} instruction (`decrement') is implemented as follows: 186 186 187 187 \begin{lstlisting} … … 202 202 \label{subsect.assembly.code.semantics} 203 203 204 An assembly program is a list of potentially labelled pseudoinstructions, bundled with a preamble consisting of a list of symbolic names for locations in data memory .204 An assembly program is a list of potentially labelled pseudoinstructions, bundled with a preamble consisting of a list of symbolic names for locations in data memory (i.e. global variables). 205 205 Pseudoinstructions are implemented as an inductive type: 206 206 \begin{lstlisting} … … 219 219 Execution of pseudoinstructions is a function from \texttt{PseudoStatus} to \texttt{PseudoStatus}. 220 220 Both \texttt{Status} and \texttt{PseudoStatus} are instances of a more general type parameterised over the representation of code memory. 221 The more general type will be crucial to share most of the semantics of the 222 two languages. 221 The more general type is crucial for sharing the majority of the semantics of the two languages. 223 222 224 223 Emulation for pseudoinstructions is handled by \texttt{execute\_1\_pseudo\_instruction}: … … 233 232 234 233 The costing returns \emph{pairs} of natural numbers because, in the case of expanding conditional jumps to labels, the expansion of the `true branch' and `false branch' may differ in execution time. 235 The \texttt{ticks1} function we already seen used to increment the machine 236 clock is determined, for the assembly language, from the costing function. 237 It is instead fixed and precomputed for machine code. 234 The \texttt{add\_ticks1} function, which we have already seen used to increment the machine clock above, is determined for the assembly language from the costing function. 238 235 239 236 Execution proceeds by first fetching from pseudo-code memory using the program counter---treated as an index into the pseudoinstruction list. … … 267 264 Each of these three instructions expects arguments in different sizes and behaves in markedly different ways: \texttt{SJMP} may only perform a `local jump'; \texttt{LJMP} may jump to any address in the MCS-51's memory space and \texttt{AJMP} may jump to any address in the current memory page. 268 265 Consequently, the size of each opcode is different, and to squeeze as much code as possible into the MCS-51's limited code memory, the smallest possible opcode that will suffice should be selected. 269 This is a well known problem to assembler writers who target RISC architectures.270 266 271 267 Similarly, a conditional pseudojump must be translated potentially into a configuration of machine code instructions, depending on the distance to the jump's target. … … 303 299 The input \texttt{pi} is the pseudoinstruction to be expanded and is found at address \texttt{ppc} in the assembly program. 304 300 The policy is defined using the two functions \texttt{sigma} and \texttt{policy}, of which \texttt{sigma} is the most interesting. 305 The function $\sigma$maps pseudo program counters to program counters: the encoding of the expansion of the pseudoinstruction found at address \texttt{a} in the assembly code should be placed into code memory at address \texttt{sigma(a)}.301 The function \texttt{sigma} maps pseudo program counters to program counters: the encoding of the expansion of the pseudoinstruction found at address \texttt{a} in the assembly code should be placed into code memory at address \texttt{sigma(a)}. 306 302 Of course this is possible only if the policy is correct, which means that the encoding of consecutive assembly instructions must be consecutive in code memory. 307 303 \begin{displaymath} … … 311 307 Note that the entanglement we hinted at is only partially solved in this way: the assembler code can ignore the implementation details of the algorithm that finds a policy; 312 308 however, the algorithm that finds a policy must know the exact behaviour of the assembly program because it needs to predict the way the assembly will expand and encode pseudoinstructions, once fed with a policy. 313 A companion paperto this one~\cite{boender:correctness:2012} certifies an algorithm that finds branch displacement policies for the assembler described in this paper.309 A companion submission to this one~\cite{boender:correctness:2012} certifies an algorithm that finds branch displacement policies for the assembler described in this paper. 314 310 315 311 The \texttt{expand\_pseudo\_instruction} function uses the \texttt{sigma} map to determine the size of jump required when expanding pseudojumps, computing the jump size by examining the size of the differences between program counters. … … 328 324 By `total correctness', we mean that the assembly process never fails when provided with a correct policy and that the process does not change the semantics of a certain class of well behaved assembly programs. 329 325 330 The aim of this section is to prove the following informal statement: when we fetch an assembly pseudoinstruction \texttt{I} at address \texttt{ppc}, then we can fetch the expanded pseudoinstruction(s) \texttt{[J1, \ldots, Jn] = fetch\_pseudo\_instruction \ldots\ I\ ppc} from \texttt{sigma(ppc)} in the code memory obtained loading the assembled object code. 331 326 The aim of this section is to prove the following informal statement: when we fetch an assembly pseudoinstruction \texttt{I} at address \texttt{ppc}, then we can fetch the expanded pseudoinstruction(s) \texttt{[J1, \ldots, Jn] = fetch\_pseudo\_instruction \ldots\ I\ ppc} from \texttt{sigma(ppc)} in the code memory obtained by loading the assembled object code. 332 327 This constitutes the first major step in the proof of correctness of the assembler, the next one being the simulation of \texttt{I} by \texttt{[J1, \ldots, Jn]} (see Subsection~\ref{subsect.total.correctness.for.well.behaved.assembly.programs}). 333 328 … … 355 350 Then, indexing into this list with any natural number \texttt{j} less than the length of \texttt{a} gives the same result as indexing into \texttt{assembled} with \texttt{sigma(ppc)} (the program counter pointing to the start of the expansion in \texttt{assembled}) plus \texttt{j}. 356 351 357 Essentially the lemma above states that the \texttt{assembly} function correctly expands pseudoinstructions .352 Essentially the lemma above states that the \texttt{assembly} function correctly expands pseudoinstructions, and that the expanded instruction reside consecutively in memory. 358 353 This result is lifted from lists of bytes into a result on tries of bytes (i.e. code memories), using an additional lemma: \texttt{assembly\_ok}. 359 354 … … 397 392 \end{lstlisting} 398 393 Here, \texttt{l} is the number of machine code instructions the pseudoinstruction at hand has been expanded into. 399 We assemble a single pseudoinstruction with \texttt{assembly\_1\_pseudoinstruction}, which internally calls \texttt{ jump\_expansion} and \texttt{expand\_pseudo\_instruction}.394 We assemble a single pseudoinstruction with \texttt{assembly\_1\_pseudoinstruction}, which internally calls \texttt{expand\_pseudo\_instruction}. 400 395 The function \texttt{fetch\_many} fetches multiple machine code instructions from code memory and performs some routine checks. 401 396 … … 427 422 We read \texttt{fetch\_assembly\_pseudo2} as follows. 428 423 Suppose we are able to successfully assemble an assembly program using \texttt{assembly} and produce a code memory, \texttt{cmem}. 429 Then, fetching a pseudoinstruction from the pseudo-code memory at address \texttt{ppc} corresponds to fetching a sequence of instructions from the real code memory using $\sigma$to expand pseudoinstructions.424 Then, fetching a pseudoinstruction from the pseudo-code memory at address \texttt{ppc} corresponds to fetching a sequence of instructions from the real code memory using \texttt{sigma} to expand pseudoinstructions. 430 425 The fetched sequence corresponds to the expansion, according to \texttt{sigma}, of the pseudoinstruction. 431 426 … … 456 451 In contrast, in this paper we take a different approach. 457 452 We trace memory locations (and, potentially, registers) that contain memory addresses. 458 We then prove that only those assembly programs that use addresses in `safe' ways have their semantics preserved by the assembly process---a sort of type system sitting atop memory.453 We then prove that only those assembly programs that use addresses in `safe' ways have their semantics preserved by the assembly process---a sort of dynamic type system sitting atop memory. 459 454 460 455 We believe that this approach is more flexible when compared to the traditional approach, as in principle it allows us to introduce some permitted \emph{benign} manipulations of addresses that the traditional approach, using opaque addresses, cannot handle, therefore expanding the set of input programs that can be assembled correctly. … … 462 457 463 458 Our analogue of the semantic function above is then merely a wrapper around the function that implements the semantics of machine code, paired with a function that keeps track of addresses. 464 This permits a large amount of code reuse, as the semantics of pseudo- and machine code isessentially shared.465 The only thing that changes a sthe assembly level is the presence of the new tracking function.459 This permits a large amount of code reuse, as the semantics of pseudo- and machine code are essentially shared. 460 The only thing that changes at the assembly level is the presence of the new tracking function. 466 461 467 462 However, with this approach we must detect (at run time) programs that manipulate addresses in well behaved ways, according to some approximation of well-behavedness. … … 479 474 480 475 The \texttt{low\_internal\_ram\_of\_pseudo\_low\_internal\_ram} function converts the lower internal RAM of a \texttt{PseudoStatus} into the lower internal RAM of a \texttt{Status}. 481 A similar function exists for high erinternal RAM.476 A similar function exists for high internal RAM. 482 477 Note that both RAM segments are indexed using addresses 7-bits long. 483 478 The function is currently axiomatised, and an associated set of axioms prescribe the behaviour of the function: … … 486 481 internal_pseudo_address_map$\rightarrow$BitVectorTrie Byte 7$\rightarrow$BitVectorTrie Byte 7. 487 482 \end{lstlisting} 488 Another pair of axioms precisely describes the supposed behaviour of \texttt{low\_internal\_ram\_of\_pseudo\_low\_internal\_ram} and its high internal ram counterpart.489 483 490 484 Next, we are able to translate \texttt{PseudoStatus} records into \texttt{Status} records using \texttt{status\_of\_pseudo\_status}. … … 535 529 \end{lstlisting} 536 530 The statement is standard for forward simulation, but restricted to \texttt{PseudoStatuses} \texttt{ps} whose next instruction to be executed is well-behaved with respect to the \texttt{internal\_pseudo\_address\_map} \texttt{M}. 537 Further, we explicitly require sproof that our policy is correct and the pseudo program counter lies within the bounds of the program.531 Further, we explicitly require proof that our policy is correct and the pseudo program counter lies within the bounds of the program. 538 532 Theorem \texttt{main\_thm} establishes the total correctness of the assembly process and can simply be lifted to the forward simulation of an arbitrary number of well behaved steps on the assembly program. 539 533 … … 545 539 546 540 We are proving the total correctness of an assembler for MCS-51 assembly language. 547 In particular, our assembly language feature dlabels, arbitrary conditional and unconditional jumps to labels, global data and instructions for moving this data into the MCS-51's single 16-bit register.541 In particular, our assembly language features labels, arbitrary conditional and unconditional jumps to labels, global data and instructions for moving this data into the MCS-51's single 16-bit register. 548 542 Expanding these pseudoinstructions into machine code instructions is not trivial, and the proof that the assembly process is `correct', in that the semantics of a subset of assembly programs are not changed is complex. 549 543 550 544 The formalisation is a key component of the CerCo project, which aims to produce a verified concrete complexity preserving compiler for a large subset of the C programming language. 551 The verified assembler, complete with the underlying formalisation of the semantics of MCS-51 machine code (described fully in~\cite{mulligan:executable:2011}), will form the bedrock layer upon which the rest of the CerCo project will build its verified compiler platform.545 The verified assembler, complete with the underlying formalisation of the semantics of MCS-51 machine code, will form the bedrock layer upon which the rest of the CerCo project will build its verified compiler platform. 552 546 553 547 It is interesting to compare our work to an `industrial grade' assembler for the MCS-51: SDCC~\cite{sdcc:2011}. … … 557 551 However, this comes at the expense of assembler completeness: the generated program may be too large to fit into code memory. 558 552 In this respect, there is a trade-off between the completeness of the assembler and the efficiency of the assembled program. 559 The definition and proof of a terminating, correct jump expansion policy is described in a companion publication to this one .553 The definition and proof of a terminating, correct jump expansion policy is described in a companion publication to this one~\cite{boender:correctness:2012}. 560 554 561 555 Aside from their application in verified compiler projects such as CerCo and CompCert, verified assemblers such as ours could also be applied to the verification of operating system kernels. … … 575 569 The second purpose is to single out the sources of incompleteness. 576 570 By abstracting our functions over the dependent type of correct policies, we were able to manifest the fact that the compiler never refuses to compile a program where a correct policy exists. 577 This also allowed to simplify the initial proof by dropping lemmas establishing that one function fails if and only if some other onedoes so.578 579 Finally, dependent types, together with Matita's liberal system of coercions, allow to simulate almost entirely in user spacethe proof methodology ``Russell'' of Sozeau~\cite{sozeau:subset:2006}.580 However, not every proof has been donethis way: we only used this style to prove that a function satisfies a specification that only involves that function in a significant way.571 This also allowed to simplify the initial proof by dropping lemmas establishing that one function fails if and only if some previous function does so. 572 573 Finally, dependent types, together with Matita's liberal system of coercions, allow us to simulate almost entirely---in user space---the proof methodology ``Russell'' of Sozeau~\cite{sozeau:subset:2006}. 574 However, not every proof has been carried out in this way: we only used this style to prove that a function satisfies a specification that only involves that function in a significant way. 581 575 For example, it would be unnatural to see the proof that fetch and assembly commute as the specification of one of the two functions. 582 576 … … 613 607 The low ratio between the number of lines of code and the number of lines of proof is unusual. 614 608 It is justified by the fact that the pseudo-assembly and the assembly language share most constructs and that large parts of the semantics are also shared. 615 Th usmany lines of code are required to describe the complex semantics of the processor, but, for the shared cases, the proof of preservation of the semantics is essentially trivial.609 Therefore many lines of code are required to describe the complex semantics of the processor, but, for the shared cases, the proof of preservation of the semantics is essentially trivial. 616 610 617 611 \bibliography{cpp-2012-asm.bib}
Note: See TracChangeset
for help on using the changeset viewer.