00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032 #ifndef _RTAI_SHM_H
00033 #define _RTAI_SHM_H
00034
00035
00036
00037
00038 #define GLOBAL_HEAP_ID 0x9ac6d9e7 // nam2num("RTGLBH");
00039
00040 #define USE_VMALLOC 0
00041 #define USE_GFP_KERNEL 1
00042 #define USE_GFP_ATOMIC 2
00043 #define USE_GFP_DMA 3
00044
00045
00046
00047
00048
00049
00050
00051
00052
00053
00054
00055
00056
00057
00058
00059
00060
00061
00062
00063
00064
00065
00066 #define rtai_kmalloc(name, size) \
00067 rt_shm_alloc(name, size, USE_VMALLOC) // legacy
00068
00069
00070
00071
00072
00073
00074
00075
00076
00077
00078
00079
00080
00081
00082
00083
00084
00085
00086 #define rtai_kfree(name) \
00087 rt_shm_free(name) // legacy
00088
00089 #if defined(__KERNEL__)
00090
00091 #include <linux/module.h>
00092 #include <linux/version.h>
00093 #include <linux/vmalloc.h>
00094 #include <linux/mm.h>
00095
00096 #if 0
00097 #if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,0)
00098 #include <linux/wrapper.h>
00099 #else
00100 #include <linux/mm.h>
00101 #define mem_map_reserve(p) SetPageReserved(p)
00102 #define mem_map_unreserve(p) ClearPageReserved(p)
00103 #endif
00104 #endif
00105
00106 #define UVIRT_TO_KVA(adr) uvirt_to_kva(pgd_offset_k(adr), (adr))
00107
00108 #if LINUX_VERSION_CODE > KERNEL_VERSION(2,6,10)
00109 static inline int remap_page_range(struct vm_area_struct *vma, unsigned long uvaddr, unsigned long paddr, unsigned long size, pgprot_t prot)
00110 {
00111 return remap_pfn_range(vma, uvaddr, paddr >> PAGE_SHIFT, size, prot);
00112 }
00113 #endif
00114
00115 #include <rtai.h>
00116
00117
00118 #include <rtai_malloc.h>
00119
00120 #ifndef CONFIG_MMU
00121
00122 static inline unsigned long uvirt_to_kva(pgd_t *pgd, unsigned long adr)
00123 {
00124 return adr;
00125 }
00126
00127 #else
00128
00129 static inline unsigned long uvirt_to_kva(pgd_t *pgd, unsigned long adr)
00130 {
00131 if (!pgd_none(*pgd) && !pgd_bad(*pgd)) {
00132 pmd_t *pmd;
00133 #if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,11)
00134 pmd = pmd_offset(pgd, adr);
00135 #else
00136 pmd = pmd_offset(pud_offset(pgd, adr), adr);
00137 #endif
00138 if (!pmd_none(*pmd)) {
00139 pte_t *ptep, pte;
00140 #if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,0)
00141 ptep = pte_offset(pmd, adr);
00142 #else
00143 ptep = pte_offset_kernel(pmd, adr);
00144 #endif
00145 pte = *ptep;
00146 if (pte_present(pte)) {
00147 return (((unsigned long)page_address(pte_page(pte))) | (adr & (PAGE_SIZE - 1)));
00148 }
00149 }
00150 }
00151 return 0UL;
00152 }
00153
00154 static inline unsigned long kvirt_to_pa(unsigned long adr)
00155 {
00156 return virt_to_phys((void *)uvirt_to_kva(pgd_offset_k(adr), adr));
00157 }
00158
00159 #endif
00160
00161 #ifdef __cplusplus
00162 extern "C" {
00163 #endif
00164
00165 int __rtai_shm_init(void);
00166
00167 void __rtai_shm_exit(void);
00168
00169 void *rt_shm_alloc(unsigned long name,
00170 int size,
00171 int suprt);
00172
00173 #define rt_shm_alloc_adr(adr, name, size) \
00174 rt_shm_alloc(name, size, suprt)
00175
00176 RTAI_SYSCALL_MODE int rt_shm_free(unsigned long name);
00177
00178 void *rt_heap_open(unsigned long name,
00179 int size,
00180 int suprt);
00181
00182 #define rt_heap_open_adr(adr, name, size, suprt) \
00183 rt_heap_open(name, size, suprt)
00184
00185 RTAI_SYSCALL_MODE void *rt_halloc(int size);
00186
00187 RTAI_SYSCALL_MODE void rt_hfree(void *addr);
00188
00189 RTAI_SYSCALL_MODE void *rt_named_halloc(unsigned long name, int size);
00190
00191 RTAI_SYSCALL_MODE void rt_named_hfree(void *addr);
00192
00193 void *rt_named_malloc(unsigned long name,
00194 int size);
00195
00196 void rt_named_free(void *addr);
00197
00198 void *rvmalloc(unsigned long size);
00199
00200 void rvfree(void *mem,
00201 unsigned long size);
00202
00203 int rvmmap(void *mem,
00204 unsigned long memsize,
00205 struct vm_area_struct *vma);
00206
00207 void *rkmalloc(int *size,
00208 int suprt);
00209
00210 void rkfree(void *mem,
00211 unsigned long size);
00212
00213 int rkmmap(void *mem,
00214 unsigned long memsize,
00215 struct vm_area_struct *vma);
00216
00217 #ifdef __cplusplus
00218 }
00219 #endif
00220
00221 #else
00222
00223 #include <fcntl.h>
00224 #include <unistd.h>
00225 #include <sys/mman.h>
00226 #include <sys/ioctl.h>
00227 #include <rtai_lxrt.h>
00228
00229
00230
00231 #define RTAI_SHM_DEV "/dev/rtai_shm"
00232
00233 RTAI_PROTO (void *, _rt_shm_alloc, (void *start, unsigned long name, int size, int suprt, int isheap))
00234 {
00235 int hook;
00236 void *adr = NULL;
00237
00238 if ((hook = open(RTAI_SHM_DEV, O_RDWR)) <= 0) {
00239 return NULL;
00240 } else {
00241 struct { unsigned long name, arg, suprt; } arg = { name, size, suprt };
00242 #ifdef SHM_USE_LXRT
00243 if ((size = rtai_lxrt(BIDX, SIZARG, SHM_ALLOC, &arg).i[LOW])) {
00244 #else
00245 if ((size = ioctl(hook, SHM_ALLOC, (unsigned long)(&arg)))) {
00246 #endif
00247 if ((adr = mmap(start, size, PROT_WRITE | PROT_READ, MAP_SHARED | MAP_LOCKED, hook, 0)) == MAP_FAILED) {;
00248 #ifdef SHM_USE_LXRT
00249 rtai_lxrt(BIDX, sizeof(name), SHM_FREE, &name);
00250 #else
00251 ioctl(hook, SHM_FREE, &name);
00252 #endif
00253 } else if (isheap) {
00254 arg.arg = (unsigned long)adr;
00255 #ifdef SHM_USE_LXRT
00256 rtai_lxrt(BIDX, SIZARG, HEAP_SET, &arg);
00257 #else
00258 ioctl(hook, HEAP_SET, &arg);
00259 #endif
00260 }
00261 }
00262 }
00263 close(hook);
00264 return adr;
00265 }
00266
00267 #define rt_shm_alloc(name, size, suprt) \
00268 _rt_shm_alloc(0, name, size, suprt, 0)
00269
00270 #define rt_heap_open(name, size, suprt) \
00271 _rt_shm_alloc(0, name, size, suprt, 1)
00272
00273
00274
00275
00276
00277
00278
00279
00280
00281
00282
00283
00284
00285
00286
00287
00288
00289
00290
00291
00292
00293
00294
00295
00296
00297 #define rtai_malloc(name, size) \
00298 _rt_shm_alloc(0, name, size, USE_VMALLOC, 0) // legacy
00299
00300
00301
00302
00303
00304
00305
00306
00307
00308
00309
00310
00311
00312
00313
00314
00315
00316
00317
00318
00319
00320
00321
00322
00323
00324
00325
00326
00327
00328
00329
00330
00331
00332
00333
00334
00335
00336
00337
00338
00339
00340
00341
00342
00343 #define rt_shm_alloc_adr(start_address, name, size, suprt) \
00344 _rt_shm_alloc(start_address, name, size, suprt, 0)
00345
00346 #define rt_heap_open_adr(start, name, size, suprt) \
00347 _rt_shm_alloc(start, name, size, suprt, 1)
00348
00349
00350
00351
00352
00353
00354
00355
00356
00357
00358
00359
00360
00361
00362
00363
00364
00365
00366
00367
00368
00369
00370
00371
00372 #define rtai_malloc_adr(start_address, name, size) \
00373 _rt_shm_alloc(start_address, name, size, USE_VMALLOC, 0) // legacy
00374
00375 RTAI_PROTO(int, rt_shm_free, (unsigned long name))
00376 {
00377 int hook, size;
00378 struct { void *nameadr; } arg = { &name };
00379 if ((hook = open(RTAI_SHM_DEV, O_RDWR)) <= 0) {
00380 return 0;
00381 }
00382
00383
00384 #ifdef SHM_USE_LXRT
00385 if ((size = rtai_lxrt(BIDX, SIZARG, SHM_SIZE, &arg).i[LOW])) {
00386 #else
00387 if ((size = ioctl(hook, SHM_SIZE, (unsigned long)&arg))) {
00388 #endif
00389 if (munmap((void *)name, size)) {
00390 size = 0;
00391 }
00392 }
00393 close(hook);
00394 return size;
00395 }
00396
00397
00398
00399
00400
00401
00402
00403
00404
00405
00406
00407
00408
00409
00410
00411
00412
00413
00414
00415
00416 #define rtai_free(name, adr) \
00417 rt_shm_free(name) // legacy
00418
00419 RTAI_PROTO(void *, rt_halloc, (int size))
00420 {
00421 struct { long size; } arg = { size };
00422 return rtai_lxrt(BIDX, SIZARG, HEAP_ALLOC, &arg).v[LOW];
00423 }
00424
00425 RTAI_PROTO(void, rt_hfree, (void *addr))
00426 {
00427 struct { void *addr; } arg = { addr };
00428 rtai_lxrt(BIDX, SIZARG, HEAP_FREE, &arg);
00429 }
00430
00431 RTAI_PROTO(void *, rt_named_halloc, (unsigned long name, int size))
00432 {
00433 struct { unsigned long name; long size; } arg = { name, size };
00434 return rtai_lxrt(BIDX, SIZARG, HEAP_NAMED_ALLOC, &arg).v[LOW];
00435 }
00436
00437 RTAI_PROTO(void, rt_named_hfree, (void *addr))
00438 {
00439 struct { void *addr; } arg = { addr };
00440 rtai_lxrt(BIDX, SIZARG, HEAP_NAMED_FREE, &arg);
00441 }
00442
00443 RTAI_PROTO(void *, rt_malloc, (int size))
00444 {
00445 struct { long size; } arg = { size };
00446 return rtai_lxrt(BIDX, SIZARG, MALLOC, &arg).v[LOW];
00447 }
00448
00449 RTAI_PROTO(void, rt_free, (void *addr))
00450 {
00451 struct { void *addr; } arg = { addr };
00452 rtai_lxrt(BIDX, SIZARG, FREE, &arg);
00453 }
00454
00455 RTAI_PROTO(void *, rt_named_malloc, (unsigned long name, int size))
00456 {
00457 struct { unsigned long name; long size; } arg = { name, size };
00458 return rtai_lxrt(BIDX, SIZARG, NAMED_MALLOC, &arg).v[LOW];
00459 }
00460
00461 RTAI_PROTO(void, rt_named_free, (void *addr))
00462 {
00463 struct { void *addr; } arg = { addr };
00464 rtai_lxrt(BIDX, SIZARG, NAMED_FREE, &arg);
00465 }
00466
00467 #endif
00468
00469
00470
00471
00472
00473
00474
00475
00476
00477
00478
00479
00480
00481
00482
00483
00484
00485
00486
00487
00488
00489
00490 #define rt_heap_close(name, adr) rt_shm_free(name)
00491
00492
00493 #define rt_heap_init rt_heap_open
00494 #define rt_heap_create rt_heap_open
00495 #define rt_heap_acquire rt_heap_open
00496 #define rt_heap_init_adr rt_heap_open_adr
00497 #define rt_heap_create_adr rt_heap_open_adr
00498 #define rt_heap_acquire_adr rt_heap_open_adr
00499
00500 #define rt_heap_delete rt_heap_close
00501 #define rt_heap_destroy rt_heap_close
00502 #define rt_heap_release rt_heap_close
00503
00504
00505
00506
00507
00508
00509
00510
00511
00512
00513
00514
00515
00516
00517
00518
00519
00520
00521 #define rt_global_heap_open() rt_heap_open(GLOBAL_HEAP_ID, 0, 0)
00522
00523
00524
00525
00526
00527
00528
00529
00530
00531
00532
00533
00534
00535
00536
00537
00538
00539 #define rt_global_heap_close() rt_heap_close(GLOBAL_HEAP_ID, 0)
00540
00541
00542
00543 #endif