1 /*
2  * Copyright 2016, The Android Open Source Project
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 // A brief overview of APF:
18 //
19 // APF machine is composed of:
20 //  1. A read-only program consisting of bytecodes as described below.
21 //  2. Two 32-bit registers, called R0 and R1.
22 //  3. Sixteen 32-bit memory slots.
23 //  4. A read-only packet.
24 // The program is executed by the interpreter below and parses the packet
25 // to determine if the application processor (AP) should be woken up to
26 // handle the packet or if can be dropped.
27 //
28 // APF bytecode description:
29 //
30 // The APF interpreter uses big-endian byte order for loads from the packet
31 // and for storing immediates in instructions.
32 //
33 // Each instruction starts with a byte composed of:
34 //  Top 5 bits form "opcode" field, see *_OPCODE defines below.
35 //  Next 2 bits form "size field", which indicate the length of an immediate
36 //  value which follows the first byte.  Values in this field:
37 //                 0 => immediate value is 0 and no bytes follow.
38 //                 1 => immediate value is 1 byte big.
39 //                 2 => immediate value is 2 bytes big.
40 //                 3 => immediate value is 4 bytes big.
41 //  Bottom bit forms "register" field, which indicates which register this
42 //  instruction operates on.
43 //
44 //  There are three main categories of instructions:
45 //  Load instructions
46 //    These instructions load byte(s) of the packet into a register.
47 //    They load either 1, 2 or 4 bytes, as determined by the "opcode" field.
48 //    They load into the register specified by the "register" field.
49 //    The immediate value that follows the first byte of the instruction is
50 //    the byte offset from the begining of the packet to load from.
51 //    There are "indexing" loads which add the value in R1 to the byte offset
52 //    to load from. The "opcode" field determines which loads are "indexing".
53 //  Arithmetic instructions
54 //    These instructions perform simple operations, like addition, on register
55 //    values. The result of these instructions is always written into R0. One
56 //    argument of the arithmetic operation is R0's value. The other argument
57 //    of the arithmetic operation is determined by the "register" field:
58 //            If the "register" field is 0 then the immediate value following
59 //            the first byte of the instruction is used as the other argument
60 //            to the arithmetic operation.
61 //            If the "register" field is 1 then R1's value is used as the other
62 //            argument to the arithmetic operation.
63 //  Conditional jump instructions
64 //    These instructions compare register R0's value with another value, and if
65 //    the comparison succeeds, jump (i.e. adjust the program counter). The
66 //    immediate value that follows the first byte of the instruction
67 //    represents the jump target offset, i.e. the value added to the program
68 //    counter if the comparison succeeds. The other value compared is
69 //    determined by the "register" field:
70 //            If the "register" field is 0 then another immediate value
71 //            follows the jump target offset. This immediate value is of the
72 //            same size as the jump target offset, and represents the value
73 //            to compare against.
74 //            If the "register" field is 1 then register R1's value is
75 //            compared against.
76 //    The type of comparison (e.g. equal to, greater than etc) is determined
77 //    by the "opcode" field. The comparison interprets both values being
78 //    compared as unsigned values.
79 //
80 //  Miscellaneous details:
81 //
82 //  Pre-filled memory slot values
83 //    When the APF program begins execution, three of the sixteen memory slots
84 //    are pre-filled by the interpreter with values that may be useful for
85 //    programs:
86 //      Slot #13 is filled with the IPv4 header length. This value is calculated
87 //               by loading the first byte of the IPv4 header and taking the
88 //               bottom 4 bits and multiplying their value by 4. This value is
89 //               set to zero if the first 4 bits after the link layer header are
90 //               not 4, indicating not IPv4.
91 //      Slot #14 is filled with size of the packet in bytes, including the
92 //               link-layer header if any.
93 //      Slot #15 is filled with the filter age in seconds. This is the number of
94 //               seconds since the AP send the program to the chipset. This may
95 //               be used by filters that should have a particular lifetime. For
96 //               example, it can be used to rate-limit particular packets to one
97 //               every N seconds.
98 //  Special jump targets:
99 //    When an APF program executes a jump to the byte immediately after the last
100 //      byte of the progam (i.e., one byte past the end of the program), this
101 //      signals the program has completed and determined the packet should be
102 //      passed to the AP.
103 //    When an APF program executes a jump two bytes past the end of the program,
104 //      this signals the program has completed and determined the packet should
105 //      be dropped.
106 //  Jump if byte sequence doesn't match:
107 //    This is a special instruction to facilitate matching long sequences of
108 //    bytes in the packet. Initially it is encoded like a conditional jump
109 //    instruction with two exceptions:
110 //      The first byte of the instruction is always followed by two immediate
111 //        fields: The first immediate field is the jump target offset like other
112 //        conditional jump instructions. The second immediate field specifies the
113 //        number of bytes to compare.
114 //      These two immediate fields are followed by a sequence of bytes. These
115 //        bytes are compared with the bytes in the packet starting from the
116 //        position specified by the value of the register specified by the
117 //        "register" field of the instruction.
118 
119 // Number of memory slots, see ldm/stm instructions.
120 #define MEMORY_ITEMS 16
121 // Upon program execution starting some memory slots are prefilled:
122 #define MEMORY_OFFSET_IPV4_HEADER_SIZE 13 // 4*([APF_FRAME_HEADER_SIZE]&15)
123 #define MEMORY_OFFSET_PACKET_SIZE 14      // Size of packet in bytes.
124 #define MEMORY_OFFSET_FILTER_AGE 15       // Age since filter installed in seconds.
125 
126 // Leave 0 opcode unused as it's a good indicator of accidental incorrect execution (e.g. data).
127 #define LDB_OPCODE 1    // Load 1 byte from immediate offset, e.g. "ldb R0, [5]"
128 #define LDH_OPCODE 2    // Load 2 bytes from immediate offset, e.g. "ldh R0, [5]"
129 #define LDW_OPCODE 3    // Load 4 bytes from immediate offset, e.g. "ldw R0, [5]"
130 #define LDBX_OPCODE 4   // Load 1 byte from immediate offset plus register, e.g. "ldbx R0, [5]R0"
131 #define LDHX_OPCODE 5   // Load 2 byte from immediate offset plus register, e.g. "ldhx R0, [5]R0"
132 #define LDWX_OPCODE 6   // Load 4 byte from immediate offset plus register, e.g. "ldwx R0, [5]R0"
133 #define ADD_OPCODE 7    // Add, e.g. "add R0,5"
134 #define MUL_OPCODE 8    // Multiply, e.g. "mul R0,5"
135 #define DIV_OPCODE 9    // Divide, e.g. "div R0,5"
136 #define AND_OPCODE 10   // And, e.g. "and R0,5"
137 #define OR_OPCODE 11    // Or, e.g. "or R0,5"
138 #define SH_OPCODE 12    // Left shift, e.g, "sh R0, 5" or "sh R0, -5" (shifts right)
139 #define LI_OPCODE 13    // Load immediate, e.g. "li R0,5" (immediate encoded as signed value)
140 #define JMP_OPCODE 14   // Unconditional jump, e.g. "jmp label"
141 #define JEQ_OPCODE 15   // Compare equal and branch, e.g. "jeq R0,5,label"
142 #define JNE_OPCODE 16   // Compare not equal and branch, e.g. "jne R0,5,label"
143 #define JGT_OPCODE 17   // Compare greater than and branch, e.g. "jgt R0,5,label"
144 #define JLT_OPCODE 18   // Compare less than and branch, e.g. "jlt R0,5,label"
145 #define JSET_OPCODE 19  // Compare any bits set and branch, e.g. "jset R0,5,label"
146 #define JNEBS_OPCODE 20 // Compare not equal byte sequence, e.g. "jnebs R0,5,label,0x1122334455"
147 #define EXT_OPCODE 21   // Immediate value is one of *_EXT_OPCODE
148 // Extended opcodes. These all have an opcode of EXT_OPCODE
149 // and specify the actual opcode in the immediate field.
150 #define LDM_EXT_OPCODE 0   // Load from memory, e.g. "ldm R0,5"
151   // Values 0-15 represent loading the different memory slots.
152 #define STM_EXT_OPCODE 16  // Store to memory, e.g. "stm R0,5"
153   // Values 16-31 represent storing to the different memory slots.
154 #define NOT_EXT_OPCODE 32  // Not, e.g. "not R0"
155 #define NEG_EXT_OPCODE 33  // Negate, e.g. "neg R0"
156 #define SWAP_EXT_OPCODE 34 // Swap, e.g. "swap R0,R1"
157 #define MOV_EXT_OPCODE 35  // Move, e.g. "move R0,R1"
158 
159 #define EXTRACT_OPCODE(i) (((i) >> 3) & 31)
160 #define EXTRACT_REGISTER(i) ((i) & 1)
161 #define EXTRACT_IMM_LENGTH(i) (((i) >> 1) & 3)
162