comm_os_linux.h 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693
  1. /*
  2. * Linux 2.6.32 and later Kernel module for VMware MVP PVTCP Server
  3. *
  4. * Copyright (C) 2010-2013 VMware, Inc. All rights reserved.
  5. *
  6. * This program is free software; you can redistribute it and/or modify it
  7. * under the terms of the GNU General Public License version 2 as published by
  8. * the Free Software Foundation.
  9. *
  10. * This program is distributed in the hope that it will be useful, but WITHOUT
  11. * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  12. * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
  13. * more details.
  14. *
  15. * You should have received a copy of the GNU General Public License along with
  16. * this program; see the file COPYING. If not, write to the Free Software
  17. * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
  18. */
  19. #line 5
  20. /**
  21. * @file
  22. *
  23. * @brief Contains linux-specific type definitions and function declarations
  24. */
  25. #ifndef _COMM_OS_LINUX_H_
  26. #define _COMM_OS_LINUX_H_
  27. #include <linux/types.h>
  28. #include <linux/version.h>
  29. #include <linux/kernel.h>
  30. #include <linux/workqueue.h>
  31. #include <linux/sched.h>
  32. #include <linux/list.h>
  33. #include <linux/module.h>
  34. #include <linux/slab.h>
  35. #include <linux/cpu.h>
  36. /*
  37. * Type definitions.
  38. */
  39. typedef atomic_t CommOSAtomic;
  40. typedef spinlock_t CommOSSpinlock;
  41. typedef struct mutex CommOSMutex;
  42. typedef wait_queue_head_t CommOSWaitQueue;
  43. typedef struct delayed_work CommOSWork;
  44. typedef void (*CommOSWorkFunc)(CommOSWork *work);
  45. typedef struct list_head CommOSList;
  46. typedef struct module *CommOSModule;
  47. /*
  48. * Initializers.
  49. */
  50. #define CommOSSpinlock_Define DEFINE_SPINLOCK
  51. #define COMM_OS_DOLOG(...) pr_info(__VA_ARGS__)
  52. /**
  53. * @brief Logs given arguments in debug builds.
  54. */
  55. #if defined(COMM_OS_DEBUG)
  56. #define CommOS_Debug(args) do { COMM_OS_DOLOG args ; } while (0)
  57. #else
  58. #define CommOS_Debug(args)
  59. #endif
  60. /**
  61. * @brief Logs given arguments.
  62. */
  63. #define CommOS_Log(args) do { COMM_OS_DOLOG args ; } while (0)
  64. /**
  65. * @brief Logs function name and location.
  66. */
  67. #if defined(COMM_OS_TRACE)
  68. #define TRACE(ptr) \
  69. CommOS_Debug(("%p:%s: at [%s:%d] with arg ptr [0x%p].\n", current, \
  70. __func__, __FILE__, __LINE__, (ptr)))
  71. #else
  72. #define TRACE(ptr)
  73. #endif
  74. /**
  75. * @brief Write atomic variable
  76. * @param[in,out] atomic variable to write
  77. * @param val new value
  78. */
  79. static inline void
  80. CommOS_WriteAtomic(CommOSAtomic *atomic,
  81. int val)
  82. {
  83. atomic_set(atomic, val);
  84. }
  85. /**
  86. * @brief Reads atomic variable
  87. * @param atomic variable to read
  88. * @return value
  89. */
  90. static inline int
  91. CommOS_ReadAtomic(CommOSAtomic *atomic)
  92. {
  93. return atomic_read(atomic);
  94. }
  95. /**
  96. * @brief Atomically add value to atomic variable, return new value.
  97. * @param[in,out] atomic variable
  98. * @param val value to add
  99. * @return new value
  100. */
  101. static inline int
  102. CommOS_AddReturnAtomic(CommOSAtomic *atomic,
  103. int val)
  104. {
  105. return atomic_add_return(val, atomic);
  106. }
  107. /**
  108. * @brief Atomically substract value from atomic variable, return new value.
  109. * @param[in,out] atomic variable
  110. * @param val value to substract
  111. * @return new value
  112. */
  113. static inline int
  114. CommOS_SubReturnAtomic(CommOSAtomic *atomic,
  115. int val)
  116. {
  117. return atomic_sub_return(val, atomic);
  118. }
  119. /**
  120. * @brief Initializes a given lock.
  121. * @param[in,out] lock lock to initialize
  122. */
  123. static inline void
  124. CommOS_SpinlockInit(CommOSSpinlock *lock)
  125. {
  126. spin_lock_init(lock);
  127. }
  128. /**
  129. * @brief Locks given lock and disables bottom half processing.
  130. * @param[in,out] lock lock to lock
  131. */
  132. static inline void
  133. CommOS_SpinLockBH(CommOSSpinlock *lock)
  134. {
  135. spin_lock_bh(lock);
  136. }
  137. /**
  138. * @brief Attempts to lock the given lock and disable BH processing.
  139. * @param[in,out] lock lock to lock
  140. * @return zero if successful, non-zero otherwise
  141. */
  142. static inline int
  143. CommOS_SpinTrylockBH(CommOSSpinlock *lock)
  144. {
  145. return !spin_trylock_bh(lock);
  146. }
  147. /**
  148. * @brief Unlocks given lock and re-enables BH processing.
  149. * @param[in,out] lock lock to unlock
  150. */
  151. static inline void
  152. CommOS_SpinUnlockBH(CommOSSpinlock *lock)
  153. {
  154. spin_unlock_bh(lock);
  155. }
  156. /**
  157. * @brief Locks the given lock.
  158. * @param[in,out] lock lock to lock
  159. */
  160. static inline void
  161. CommOS_SpinLock(CommOSSpinlock *lock)
  162. {
  163. spin_lock(lock);
  164. }
  165. /**
  166. * @brief Attempts to lock the given lock.
  167. * @param[in,out] lock lock to try-lock
  168. * @return zero if successful, non-zero otherwise
  169. */
  170. static inline int
  171. CommOS_SpinTrylock(CommOSSpinlock *lock)
  172. {
  173. return !spin_trylock(lock);
  174. }
  175. /**
  176. * @brief Unlocks given lock.
  177. * @param[in,out] lock lock to unlock
  178. */
  179. static inline void
  180. CommOS_SpinUnlock(CommOSSpinlock *lock)
  181. {
  182. spin_unlock(lock);
  183. }
  184. /**
  185. * @brief Initializes given mutex.
  186. * @param[in,out] mutex mutex to initialize
  187. */
  188. static inline void
  189. CommOS_MutexInit(CommOSMutex *mutex)
  190. {
  191. mutex_init(mutex);
  192. }
  193. /**
  194. * @brief Acquires mutex.
  195. * @param[in,out] mutex mutex to lock
  196. * @return zero if successful, non-zero otherwise (interrupted)
  197. */
  198. static inline int
  199. CommOS_MutexLock(CommOSMutex *mutex)
  200. {
  201. return mutex_lock_interruptible(mutex);
  202. }
  203. /**
  204. * @brief Acquires mutex in uninterruptible mode.
  205. * @param[in,out] mutex mutex to lock
  206. */
  207. static inline void
  208. CommOS_MutexLockUninterruptible(CommOSMutex *mutex)
  209. {
  210. mutex_lock(mutex);
  211. }
  212. /**
  213. * @brief Attempts to acquire given mutex.
  214. * @param[in,out] mutex mutex to try-lock
  215. * @return zero if successful, non-zero otherwise
  216. */
  217. static inline int
  218. CommOS_MutexTrylock(CommOSMutex *mutex)
  219. {
  220. return !mutex_trylock(mutex);
  221. }
  222. /**
  223. * @brief Releases a given mutex.
  224. * @param[in,out] mutex mutex to unlock
  225. */
  226. static inline void
  227. CommOS_MutexUnlock(CommOSMutex *mutex)
  228. {
  229. mutex_unlock(mutex);
  230. }
  231. /**
  232. * @brief Initializes a wait queue.
  233. * @param[in,out] wq workqueue to initialize
  234. */
  235. static inline void
  236. CommOS_WaitQueueInit(CommOSWaitQueue *wq)
  237. {
  238. init_waitqueue_head(wq);
  239. }
  240. /**
  241. * @brief Puts the caller on a wait queue until either of the following occurs:
  242. * - the condition function (predicate) evaluates to TRUE
  243. * - the specified timeout interval elapsed
  244. * - a signal is pending
  245. * @param[in,out] wq wait queue to put item on
  246. * @param cond predicate to test
  247. * @param condArg1 argument 1 for cond
  248. * @param condArg2 argument 2 for cond
  249. * @param[in,out] timeoutMillis timeout interval in milliseconds
  250. * @param interruptible enable/disable signal pending check
  251. * @return 1 if condition was met
  252. * 0 if the timeout interval elapsed
  253. * <0, if a signal is pending or other error set by condition
  254. * @sideeffect timeoutMillis is updated to time remaining
  255. */
  256. static inline int
  257. CommOS_DoWait(CommOSWaitQueue *wq,
  258. CommOSWaitConditionFunc cond,
  259. void *condArg1,
  260. void *condArg2,
  261. unsigned long long *timeoutMillis,
  262. int interruptible)
  263. {
  264. int rc;
  265. DEFINE_WAIT(wait);
  266. long timeout;
  267. #if defined(COMM_OS_LINUX_WAIT_WORKAROUND)
  268. long tmpTimeout;
  269. long retTimeout;
  270. const unsigned int interval = 50;
  271. #endif
  272. if (!timeoutMillis)
  273. return -1;
  274. rc = cond(condArg1, condArg2);
  275. if (rc != 0)
  276. return rc;
  277. #if defined(COMM_OS_LINUX_WAIT_WORKAROUND)
  278. timeout = msecs_to_jiffies(interval < *timeoutMillis ?
  279. interval : (unsigned int)*timeoutMillis);
  280. retTimeout = msecs_to_jiffies((unsigned int)(*timeoutMillis));
  281. for (; retTimeout >= 0; ) {
  282. prepare_to_wait(wq, &wait,
  283. (interruptible ? TASK_INTERRUPTIBLE : TASK_UNINTERRUPTIBLE));
  284. rc = cond(condArg1, condArg2);
  285. if (rc)
  286. break;
  287. if (interruptible && signal_pending(current)) {
  288. rc = -EINTR;
  289. break;
  290. }
  291. tmpTimeout = schedule_timeout(timeout);
  292. if (tmpTimeout)
  293. retTimeout -= (timeout - tmpTimeout);
  294. else
  295. retTimeout -= timeout;
  296. if (retTimeout < 0)
  297. retTimeout = 0;
  298. }
  299. finish_wait(wq, &wait);
  300. if (rc == 0) {
  301. rc = cond(condArg1, condArg2);
  302. if (rc && (retTimeout == 0))
  303. retTimeout = 1;
  304. }
  305. *timeoutMillis = (unsigned long long)jiffies_to_msecs(retTimeout);
  306. #else /* !defined(COMM_OS_LINUX_WAIT_WORKAROUND) */
  307. timeout = msecs_to_jiffies((unsigned int)(*timeoutMillis));
  308. for (;;) {
  309. prepare_to_wait(wq, &wait,
  310. (interruptible ? TASK_INTERRUPTIBLE : TASK_UNINTERRUPTIBLE));
  311. rc = cond(condArg1, condArg2);
  312. if (rc != 0)
  313. break;
  314. if (interruptible && signal_pending(current)) {
  315. rc = -EINTR;
  316. break;
  317. }
  318. timeout = schedule_timeout(timeout);
  319. if (timeout == 0) {
  320. rc = 0;
  321. break;
  322. }
  323. }
  324. finish_wait(wq, &wait);
  325. if (rc == 0) {
  326. rc = cond(condArg1, condArg2);
  327. if (rc && (timeout == 0))
  328. timeout = 1;
  329. }
  330. *timeoutMillis = (unsigned long long)jiffies_to_msecs(timeout);
  331. #endif
  332. return rc;
  333. }
  334. /**
  335. * @brief Puts the caller on a wait queue until either of the following occurs:
  336. * - the condition function (predicate) evaluates to TRUE
  337. * - the specified timeout interval elapsed
  338. * - a signal is pending
  339. * @param[in,out] wq wait queue to put item on
  340. * @param cond predicate to test
  341. * @param condArg1 argument 1 for cond
  342. * @param condArg2 argument 2 for cond
  343. * @param[in,out] timeoutMillis timeout interval in milliseconds
  344. * @return 1 if condition was met
  345. * 0 if the timeout interval elapsed
  346. * <0, if a signal is pending or other error set by condition
  347. * @sideeffect timeoutMillis is updated to time remaining
  348. */
  349. static inline int
  350. CommOS_Wait(CommOSWaitQueue *wq,
  351. CommOSWaitConditionFunc cond,
  352. void *condArg1,
  353. void *condArg2,
  354. unsigned long long *timeoutMillis)
  355. {
  356. return CommOS_DoWait(wq, cond, condArg1, condArg2, timeoutMillis, 1);
  357. }
  358. /**
  359. * @brief Puts the caller on a wait queue until either of the following occurs:
  360. * - the condition function (predicate) evaluates to TRUE
  361. * - the specified timeout interval elapsed
  362. * @param[in,out] wq wait queue to put item on
  363. * @param cond predicate to test
  364. * @param condArg1 argument 1 for cond
  365. * @param condArg2 argument 2 for cond
  366. * @param[in,out] timeoutMillis timeout interval in milliseconds
  367. * @return 1 if condition was met
  368. * 0 if the timeout interval elapsed
  369. * <0, error set by condition
  370. * @sideeffect timeoutMillis is updated to time remaining
  371. */
  372. static inline int
  373. CommOS_WaitUninterruptible(CommOSWaitQueue *wq,
  374. CommOSWaitConditionFunc cond,
  375. void *condArg1,
  376. void *condArg2,
  377. unsigned long long *timeoutMillis)
  378. {
  379. return CommOS_DoWait(wq, cond, condArg1, condArg2, timeoutMillis, 0);
  380. }
  381. /**
  382. * @brief Wakes up task(s) waiting on the given wait queue.
  383. * @param[in,out] wq wait queue.
  384. */
  385. static inline void
  386. CommOS_WakeUp(CommOSWaitQueue *wq)
  387. {
  388. wake_up(wq);
  389. }
  390. /**
  391. * @brief Allocates kernel memory of specified size; does not sleep.
  392. * @param size size to allocate.
  393. * @return Address of allocated memory or NULL if the allocation fails.
  394. */
  395. static inline void *
  396. CommOS_KmallocNoSleep(unsigned int size)
  397. {
  398. return kmalloc(size, GFP_ATOMIC);
  399. }
  400. /**
  401. * @brief Allocates kernel memory of specified size; may sleep.
  402. * @param size size to allocate.
  403. * @return Address of allocated memory or NULL if the allocation fails.
  404. */
  405. static inline void *
  406. CommOS_Kmalloc(unsigned int size)
  407. {
  408. return kmalloc(size, GFP_KERNEL);
  409. }
  410. /**
  411. * @brief Frees previously allocated kernel memory.
  412. * @param obj object to free.
  413. */
  414. static inline void
  415. CommOS_Kfree(void *obj)
  416. {
  417. kfree(obj);
  418. }
  419. /**
  420. * @brief Yields the current cpu to other runnable tasks.
  421. */
  422. static inline void
  423. CommOS_Yield(void)
  424. {
  425. cond_resched();
  426. }
  427. /**
  428. * @brief Gets the current time in milliseconds.
  429. * @return Current time in milliseconds, with precision of at most one tick.
  430. */
  431. static inline unsigned long long
  432. CommOS_GetCurrentMillis(void)
  433. {
  434. return (unsigned long long)jiffies_to_msecs(jiffies);
  435. }
  436. /**
  437. * @brief Initializes given list.
  438. * @param list list to initialize.
  439. */
  440. static inline void
  441. CommOS_ListInit(CommOSList *list)
  442. {
  443. INIT_LIST_HEAD(list);
  444. }
  445. /**
  446. * @brief Tests if list is empty.
  447. * @param list list to test.
  448. * @return non-zero if empty, zero otherwise.
  449. */
  450. #define CommOS_ListEmpty(list) list_empty((list))
  451. /**
  452. * @brief Adds given element to beginning of list.
  453. * @param list list to add to.
  454. * @param elem element to add.
  455. */
  456. #define CommOS_ListAdd(list, elem) list_add((elem), (list))
  457. /**
  458. * @brief Adds given element to end of list.
  459. * @param list list to add to.
  460. * @param elem element to add.
  461. */
  462. #define CommOS_ListAddTail(list, elem) list_add_tail((elem), (list))
  463. /**
  464. * @brief Deletes given element from its list.
  465. * @param elem element to delete.
  466. */
  467. #define CommOS_ListDel(elem) \
  468. do { \
  469. list_del((elem)); \
  470. INIT_LIST_HEAD((elem)); \
  471. } while (0)
  472. /**
  473. * @brief Iterates over a list.
  474. * @param list list to iterate over.
  475. * @param[out] item stores next element.
  476. * @param itemListFieldName name in the item structure storing the list head.
  477. */
  478. #define CommOS_ListForEach(list, item, itemListFieldName) \
  479. list_for_each_entry((item), (list), itemListFieldName)
  480. /**
  481. * @brief Iterates safely over a list.
  482. * @param list list to iterate over.
  483. * @param[out] item stores next element. May be deleted in the loop.
  484. * @param[out] tmpItem saves iteration element.
  485. * @param itemListFieldName name in the item structure storing the list head.
  486. */
  487. #define CommOS_ListForEachSafe(list, item, tmpItem, itemListFieldName) \
  488. list_for_each_entry_safe((item), (tmpItem), (list), itemListFieldName)
  489. /**
  490. * @brief Combines two lists, adds second list to beginning of first one.
  491. * @param list list to add to.
  492. * @param list2 list to add.
  493. */
  494. #define CommOS_ListSplice(list, list2) list_splice((list2), (list))
  495. /**
  496. * @brief Combines two lists, adds second list to end of first one.
  497. * @param list list to add to.
  498. * @param list2 list to add.
  499. */
  500. #define CommOS_ListSpliceTail(list, list2) list_splice_tail((list2), (list))
  501. /**
  502. * @brief Gets current module handle.
  503. * @return module handle.
  504. */
  505. static inline CommOSModule
  506. CommOS_ModuleSelf(void)
  507. {
  508. return THIS_MODULE;
  509. }
  510. /**
  511. * @brief Retains module.
  512. * @param[in,out] module to retain.
  513. * @return zero if successful, non-zero otherwise.
  514. */
  515. static inline int
  516. CommOS_ModuleGet(CommOSModule module)
  517. {
  518. int rc = 0;
  519. if (!module)
  520. goto out;
  521. if (!try_module_get(module))
  522. rc = -1;
  523. out:
  524. return rc;
  525. }
  526. /**
  527. * @brief Releases module.
  528. * @param[in,out] module to release.
  529. */
  530. static inline void
  531. CommOS_ModulePut(CommOSModule module)
  532. {
  533. if (module)
  534. module_put(module);
  535. }
  536. /**
  537. * @brief Inserts r/w memory barrier.
  538. */
  539. #define CommOS_MemBarrier smp_mb
  540. #endif /* _COMM_OS_LINUX_H_ */