[tcp] Allow out-of-order receive queue to be discarded
[gpxe.git] / src / arch / i386 / include / librm.h
blobc6992f64395d581c30031a780b9a8dfab38331a3
1 #ifndef LIBRM_H
2 #define LIBRM_H
4 FILE_LICENCE ( GPL2_OR_LATER );
6 /* Segment selectors as used in our protected-mode GDTs.
8 * Don't change these unless you really know what you're doing.
9 */
11 #define VIRTUAL_CS 0x08
12 #define VIRTUAL_DS 0x10
13 #define PHYSICAL_CS 0x18
14 #define PHYSICAL_DS 0x20
15 #define REAL_CS 0x28
16 #define REAL_DS 0x30
17 #if 0
18 #define LONG_CS 0x38
19 #define LONG_DS 0x40
20 #endif
22 #ifndef ASSEMBLY
24 #ifdef UACCESS_LIBRM
25 #define UACCESS_PREFIX_librm
26 #else
27 #define UACCESS_PREFIX_librm __librm_
28 #endif
30 /* Variables in librm.S */
31 extern unsigned long virt_offset;
33 /**
34 * Convert physical address to user pointer
36 * @v phys_addr Physical address
37 * @ret userptr User pointer
39 static inline __always_inline userptr_t
40 UACCESS_INLINE ( librm, phys_to_user ) ( unsigned long phys_addr ) {
41 return ( phys_addr - virt_offset );
44 /**
45 * Convert user buffer to physical address
47 * @v userptr User pointer
48 * @v offset Offset from user pointer
49 * @ret phys_addr Physical address
51 static inline __always_inline unsigned long
52 UACCESS_INLINE ( librm, user_to_phys ) ( userptr_t userptr, off_t offset ) {
53 return ( userptr + offset + virt_offset );
56 static inline __always_inline userptr_t
57 UACCESS_INLINE ( librm, virt_to_user ) ( volatile const void *addr ) {
58 return trivial_virt_to_user ( addr );
61 static inline __always_inline void *
62 UACCESS_INLINE ( librm, user_to_virt ) ( userptr_t userptr, off_t offset ) {
63 return trivial_user_to_virt ( userptr, offset );
66 static inline __always_inline userptr_t
67 UACCESS_INLINE ( librm, userptr_add ) ( userptr_t userptr, off_t offset ) {
68 return trivial_userptr_add ( userptr, offset );
71 static inline __always_inline void
72 UACCESS_INLINE ( librm, memcpy_user ) ( userptr_t dest, off_t dest_off,
73 userptr_t src, off_t src_off,
74 size_t len ) {
75 trivial_memcpy_user ( dest, dest_off, src, src_off, len );
78 static inline __always_inline void
79 UACCESS_INLINE ( librm, memmove_user ) ( userptr_t dest, off_t dest_off,
80 userptr_t src, off_t src_off,
81 size_t len ) {
82 trivial_memmove_user ( dest, dest_off, src, src_off, len );
85 static inline __always_inline void
86 UACCESS_INLINE ( librm, memset_user ) ( userptr_t buffer, off_t offset,
87 int c, size_t len ) {
88 trivial_memset_user ( buffer, offset, c, len );
91 static inline __always_inline size_t
92 UACCESS_INLINE ( librm, strlen_user ) ( userptr_t buffer, off_t offset ) {
93 return trivial_strlen_user ( buffer, offset );
96 static inline __always_inline off_t
97 UACCESS_INLINE ( librm, memchr_user ) ( userptr_t buffer, off_t offset,
98 int c, size_t len ) {
99 return trivial_memchr_user ( buffer, offset, c, len );
103 /******************************************************************************
105 * Access to variables in .data16 and .text16
109 extern char *data16;
110 extern char *text16;
112 #define __data16( variable ) \
113 __attribute__ (( section ( ".data16" ) )) \
114 _data16_ ## variable __asm__ ( #variable )
116 #define __data16_array( variable, array ) \
117 __attribute__ (( section ( ".data16" ) )) \
118 _data16_ ## variable array __asm__ ( #variable )
120 #define __bss16( variable ) \
121 __attribute__ (( section ( ".bss16" ) )) \
122 _data16_ ## variable __asm__ ( #variable )
124 #define __bss16_array( variable, array ) \
125 __attribute__ (( section ( ".bss16" ) )) \
126 _data16_ ## variable array __asm__ ( #variable )
128 #define __text16( variable ) \
129 __attribute__ (( section ( ".text16.data" ) )) \
130 _text16_ ## variable __asm__ ( #variable )
132 #define __text16_array( variable, array ) \
133 __attribute__ (( section ( ".text16.data" ) )) \
134 _text16_ ## variable array __asm__ ( #variable )
136 #define __use_data16( variable ) \
137 ( * ( ( typeof ( _data16_ ## variable ) * ) \
138 & ( data16 [ ( size_t ) & ( _data16_ ## variable ) ] ) ) )
140 #define __use_text16( variable ) \
141 ( * ( ( typeof ( _text16_ ## variable ) * ) \
142 & ( text16 [ ( size_t ) & ( _text16_ ## variable ) ] ) ) )
144 #define __from_data16( pointer ) \
145 ( ( unsigned int ) \
146 ( ( ( void * ) (pointer) ) - ( ( void * ) data16 ) ) )
148 #define __from_text16( pointer ) \
149 ( ( unsigned int ) \
150 ( ( ( void * ) (pointer) ) - ( ( void * ) text16 ) ) )
152 /* Variables in librm.S, present in the normal data segment */
153 extern uint16_t rm_sp;
154 extern uint16_t rm_ss;
155 extern uint16_t __data16 ( rm_cs );
156 #define rm_cs __use_data16 ( rm_cs )
157 extern uint16_t __text16 ( rm_ds );
158 #define rm_ds __use_text16 ( rm_ds )
161 * Convert segment:offset address to user buffer
163 * @v segment Real-mode segment
164 * @v offset Real-mode offset
165 * @ret buffer User buffer
167 static inline __always_inline userptr_t
168 real_to_user ( unsigned int segment, unsigned int offset ) {
169 return ( phys_to_user ( ( segment << 4 ) + offset ) );
172 extern uint16_t copy_user_to_rm_stack ( userptr_t data, size_t size );
173 extern void remove_user_from_rm_stack ( userptr_t data, size_t size );
175 /* TEXT16_CODE: declare a fragment of code that resides in .text16 */
176 #define TEXT16_CODE( asm_code_str ) \
177 ".section \".text16\", \"ax\", @progbits\n\t" \
178 ".code16\n\t" \
179 asm_code_str "\n\t" \
180 ".code32\n\t" \
181 ".previous\n\t"
183 /* REAL_CODE: declare a fragment of code that executes in real mode */
184 #define REAL_CODE( asm_code_str ) \
185 "pushl $1f\n\t" \
186 "call real_call\n\t" \
187 "addl $4, %%esp\n\t" \
188 TEXT16_CODE ( "\n1:\n\t" \
189 asm_code_str \
190 "\n\t" \
191 "ret\n\t" )
193 /* PHYS_CODE: declare a fragment of code that executes in flat physical mode */
194 #define PHYS_CODE( asm_code_str ) \
195 "call _virt_to_phys\n\t" \
196 asm_code_str \
197 "call _phys_to_virt\n\t"
199 #endif /* ASSEMBLY */
201 #endif /* LIBRM_H */