250 likes | 363 Vues
This tutorial provides a comprehensive overview of the t-Kernel, focusing on interrupt handling, code reviews, coding examples, and Aspect-Oriented Programming (AOP). The kernel efficiently manages interrupts, allowing applications to cooperate in handling events. Key topics include the collaboration between the kernel and application, functioning of SPI interrupts, and the innovative features of the t-Kernel. With practical coding examples and data structure analyses, this session is designed for developers looking to enhance their understanding of embedded systems and AOP techniques.
E N D
Tutorial of the t-kernel(Session B)July 7, 2006 Lin Gu Dept. of Computer Science University of Virginia lingu@cs.virginia.edu http://www.cs.virginia.edu/~lg6e
Resources • Web page: http://www.cs.virginia.edu/~lg6e/t-kernel/ • Source code in the vert repository: www.sf.net/projects/vert • Please let me know your sourceforge accounts, or use the anonymous account to check out the code • Directory: /cvsroot/vert/t-kernel
Outline • Interrupt handling • Code review • Coding example • t-kernel and Aspect Oriented Programming
Interrupt Handling • The kernel monitors interrupts • The application handles interrupts • The application can use any instructions, including disabling/enabling interrupts • It is possible for the application and kernel to cooperate to handle interrupts • SPI interrupts • Byte level cooperation • Frame level cooperation
Source code tree townLogic() clockLogic() translate() translateBranch() pleaseBridge() leadOut()
Code review – Data structure typedef uint16_t * Code; typedef struct { Code codeEntry; uint8_t cWlen, /*length of the code chunk*/ cCur /* count of the current word */; } CodeChunk; CodeChunk ccRaw;
Code review – Data structure typedef struct { uint32_t lVPWCStart; uint8_t cWlen; /* length of the code chunk */ Code codeEntry; IFPMapEntry *pifpmeStanza; uint8_t cMaxStanza; uint8_t cFlag; /* TKNL_NCFLAG_* flags */ uint16_t nPgmPageNo; uint16_t nMemAreaStart; uint16_t nVoidPage[TKNL_NC_LINKIN_NUM]; } NaturalizedChunk; NaturalizedChunk ncNat;
Code review – Functions uint16_t translate (uint32_t lVPWCWanted, uint16_t nPageNo, uint16_t nSrcPage, uint8_t bRevert); • Copy/modify application code to construct a natin page. • Multiple-use • Create new natin page for a new VPC • Add a new entry point to an existing natin page • Invalidate bridging jumps in a natin page
Code review – Functions uint8_t translateBranch(); uint8_t pleaseBridge(uint16_t nDest); • Translate conventional conditional branches • Differentiate forward and backward branches
Bridging – Backward branch • The naturalizer patches the natin page • SystemCounter is 8-bit • One trap into kernel per 256 backward branches • “headmaster” does sanity check in the dispatcher ... add r0, r1 ... push r31 in r31, 0x3f push r31 lds SystemCounter inc SystemCounter sts SystemCounter brne go cli call headmaster go: pop r31 Out 0x3f, r31 pop r31 jmp HPC ... naturalizer ... Prev: add r0, r1 ... rcall branch_stub Prev (VPC) ... ... HPC dispatcher
Code review – Functions void leadOut(uint16_t nSrcPage, char bCopyLinkin); • Write or copy the link-in records • Write finis
Link-in and finis • Bridging makes direct jumps between natin pages • The incoming pages are recorded in the link-in record of the natin page • Needed for invalidating direct jumps when a natin page is changed (new entry point inserted, or swapped out) ... ... last town transition incoming page 0 incoming page 1 ... incoming page 2 branch_stub ... page 109
Code review – Functions uint16_t townLogic(); • Handle town transitions • Rewrite “rcall branch_stub” to bridging jumps
Bridging – Forward branch • The dispatcher looks up HPC for “Next” • The naturalizer patches the natin page • Handled by “townLogic” (town transition) naturalizer rcall branch_stub Next (VPC) ... ... add r0, r1 ... jmp HPC ... ... add r0, r1 ... Stack frame Stack frame HPC ret1 branch_stub: push r31 in r31, 0x3f cli call townGate branch_stub: push r31 in r31, 0x3f cli call townGate r31 ret0 dispatcher
Code review – Interrupt uint16_t clockLogic(){ uint16_t nStackHead = getAppStackHead(); IntRoom irT; ncNat.lVPWCStart = nVector[TKNL_SIG_OUTPUT_COMPARE0]; cMind |= 1; lTick++; if (checkNestInt()) return -5; irT.c1 = *((uint8_t *)(nStackHead+4)); irT.c0 = *((uint8_t *)(nStackHead+5)); irT.n0 <<= 1; pinInterruptedNatin(irT.c1); <run ncNat.lVPWCStart>; } // clockLogic
Code review – Flags commtknl.h contains system-wide flags • TKNL_PRODUCTION – Disables all debug flags • TKNL_MERGE_NATIN_PAGE – Enables merging of natin pages (add entry points to a natin page) • TKNL_GREEN_LANE – Enables VPC look-aside buffer • TKNL_VISIT_TOWN – Enables bridging • TKNL_INTERRUPT_NUM_NEST – Interrupt nesting level
t-kernel & Aspect Oriented Programming • Aspect oriented programming (AOP) • Pointcut • Advice • XPI • AspectJ, AspectC, etc. • Works on source code or compiled (existing) code • Techniques: aspect compilers, virtual machines, reverse-engineering
t-kernel & Aspect Oriented Programming • A basic Aspect framework • Pointcut, Advice, XPI • Supporting compiled (existing) code • Aspects can be propagated in the network • How to implement such a framework in the t-kernel?
t-kernel & AOP • A basic Aspect framework • Pointcut, Advice, XPI • Supporting compiled (existing) code • Aspects can be propagated in the network • How to implement such a framework in the t-kernel?
t-kernel & AOP • Program+XPI tagged program • Tags can be as simple as a number, or contain semantic information • Not limited to function calls • Advice = new code • E.g., advice1 to be attached before pointcut1 Add r0, r1 Call func1 Next: Sub r1, r2 Add r0, r1 <jp_1_start> Call func1 <jp_1_end> Next:Sub r1, r2 Pointcut: function func*
t-kernel & AOP “before” a join point • Insert the new code before the “start” tag Add r0, r1 <jp_1_start> Call func1 <jp_1_end> Next:Sub r1, r2 Add r0, r1 <jp_1_start> Call adv1 Call func1 <jp_1_end> Next: Sub r1, r2 Code modification Advice1: Call adv1
t-kernel & AOP “after” a join point • Insert the new code after the “end” tag Add r0, r1 <jp_1_start> Call adv1 Call func1 <jp_1_end> Next: Sub r1, r2 Add r0, r1 <jp_1_start> Call adv1 Call func1 Call adv2 <jp_1_end> Next: Sub r1, r2 Code modification Advice2: Call adv2
t-kernel & AOP “around” a join point • Insert the new code after the “start” tag and jump to after the “end” tag Add r0, r1 <jp_1_start> Call adv1 Call func1 Call adv2 <jp_1_end> Next: Sub r1, r2 Add r0, r1 <jp_1_start> Call adv3 jmp jp_1_end Call adv1 Call func1 Call adv2 <jp_1_end> Next: Sub r1, r2 Code modification Advice3: Call adv3
t-kernel & AOP Key function needed • Online code weaving • Invalidate old code • Generate new code based on the old code and the new advices • May be implemented by the “entry point” inserting and natin page swapping mechanism in the t-kernel • Aspect compiler • modify nesC or gcc? New compiler? Preprocessor?
t-kernel & AOP Supporting mechanisms • XPI definition language • Examples exist • Advice delivery mechanism • The t-kernel support wireless communication