n_tty.c 63 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535
  1. // SPDX-License-Identifier: GPL-1.0+
  2. /*
  3. * n_tty.c --- implements the N_TTY line discipline.
  4. *
  5. * This code used to be in tty_io.c, but things are getting hairy
  6. * enough that it made sense to split things off. (The N_TTY
  7. * processing has changed so much that it's hardly recognizable,
  8. * anyway...)
  9. *
  10. * Note that the open routine for N_TTY is guaranteed never to return
  11. * an error. This is because Linux will fall back to setting a line
  12. * to N_TTY if it can not switch to any other line discipline.
  13. *
  14. * Written by Theodore Ts'o, Copyright 1994.
  15. *
  16. * This file also contains code originally written by Linus Torvalds,
  17. * Copyright 1991, 1992, 1993, and by Julian Cowley, Copyright 1994.
  18. *
  19. * Reduced memory usage for older ARM systems - Russell King.
  20. *
  21. * 2000/01/20 Fixed SMP locking on put_tty_queue using bits of
  22. * the patch by Andrew J. Kroll <ag784@freenet.buffalo.edu>
  23. * who actually finally proved there really was a race.
  24. *
  25. * 2002/03/18 Implemented n_tty_wakeup to send SIGIO POLL_OUTs to
  26. * waiting writing processes-Sapan Bhatia <sapan@corewars.org>.
  27. * Also fixed a bug in BLOCKING mode where n_tty_write returns
  28. * EAGAIN
  29. */
  30. #include <linux/bitmap.h>
  31. #include <linux/bitops.h>
  32. #include <linux/ctype.h>
  33. #include <linux/errno.h>
  34. #include <linux/export.h>
  35. #include <linux/fcntl.h>
  36. #include <linux/file.h>
  37. #include <linux/jiffies.h>
  38. #include <linux/math.h>
  39. #include <linux/poll.h>
  40. #include <linux/ratelimit.h>
  41. #include <linux/sched.h>
  42. #include <linux/signal.h>
  43. #include <linux/slab.h>
  44. #include <linux/string.h>
  45. #include <linux/tty.h>
  46. #include <linux/types.h>
  47. #include <linux/uaccess.h>
  48. #include <linux/vmalloc.h>
  49. #include "tty.h"
  50. /*
  51. * Until this number of characters is queued in the xmit buffer, select will
  52. * return "we have room for writes".
  53. */
  54. #define WAKEUP_CHARS 256
  55. #define N_TTY_BUF_SIZE 4096
  56. /*
  57. * This defines the low- and high-watermarks for throttling and
  58. * unthrottling the TTY driver. These watermarks are used for
  59. * controlling the space in the read buffer.
  60. */
  61. #define TTY_THRESHOLD_THROTTLE 128 /* now based on remaining room */
  62. #define TTY_THRESHOLD_UNTHROTTLE 128
  63. /*
  64. * Special byte codes used in the echo buffer to represent operations
  65. * or special handling of characters. Bytes in the echo buffer that
  66. * are not part of such special blocks are treated as normal character
  67. * codes.
  68. */
  69. #define ECHO_OP_START 0xff
  70. #define ECHO_OP_MOVE_BACK_COL 0x80
  71. #define ECHO_OP_SET_CANON_COL 0x81
  72. #define ECHO_OP_ERASE_TAB 0x82
  73. #define ECHO_COMMIT_WATERMARK 256
  74. #define ECHO_BLOCK 256
  75. #define ECHO_DISCARD_WATERMARK N_TTY_BUF_SIZE - (ECHO_BLOCK + 32)
  76. struct n_tty_data {
  77. /* producer-published */
  78. size_t read_head;
  79. size_t commit_head;
  80. size_t canon_head;
  81. size_t echo_head;
  82. size_t echo_commit;
  83. size_t echo_mark;
  84. DECLARE_BITMAP(char_map, 256);
  85. /* private to n_tty_receive_overrun (single-threaded) */
  86. unsigned long overrun_time;
  87. unsigned int num_overrun;
  88. /* non-atomic */
  89. bool no_room;
  90. /* must hold exclusive termios_rwsem to reset these */
  91. unsigned char lnext:1, erasing:1, raw:1, real_raw:1, icanon:1;
  92. unsigned char push:1;
  93. /* shared by producer and consumer */
  94. u8 read_buf[N_TTY_BUF_SIZE];
  95. DECLARE_BITMAP(read_flags, N_TTY_BUF_SIZE);
  96. u8 echo_buf[N_TTY_BUF_SIZE];
  97. /* consumer-published */
  98. size_t read_tail;
  99. size_t line_start;
  100. /* # of chars looked ahead (to find software flow control chars) */
  101. size_t lookahead_count;
  102. /* protected by output lock */
  103. unsigned int column;
  104. unsigned int canon_column;
  105. size_t echo_tail;
  106. struct mutex atomic_read_lock;
  107. struct mutex output_lock;
  108. };
  109. #define MASK(x) ((x) & (N_TTY_BUF_SIZE - 1))
  110. static inline size_t read_cnt(struct n_tty_data *ldata)
  111. {
  112. return ldata->read_head - ldata->read_tail;
  113. }
  114. static inline u8 read_buf(struct n_tty_data *ldata, size_t i)
  115. {
  116. return ldata->read_buf[MASK(i)];
  117. }
  118. static inline u8 *read_buf_addr(struct n_tty_data *ldata, size_t i)
  119. {
  120. return &ldata->read_buf[MASK(i)];
  121. }
  122. static inline u8 echo_buf(struct n_tty_data *ldata, size_t i)
  123. {
  124. smp_rmb(); /* Matches smp_wmb() in add_echo_byte(). */
  125. return ldata->echo_buf[MASK(i)];
  126. }
  127. static inline u8 *echo_buf_addr(struct n_tty_data *ldata, size_t i)
  128. {
  129. return &ldata->echo_buf[MASK(i)];
  130. }
  131. /* If we are not echoing the data, perhaps this is a secret so erase it */
  132. static void zero_buffer(const struct tty_struct *tty, u8 *buffer, size_t size)
  133. {
  134. if (L_ICANON(tty) && !L_ECHO(tty))
  135. memset(buffer, 0, size);
  136. }
  137. static void tty_copy(const struct tty_struct *tty, void *to, size_t tail,
  138. size_t n)
  139. {
  140. struct n_tty_data *ldata = tty->disc_data;
  141. size_t size = N_TTY_BUF_SIZE - tail;
  142. void *from = read_buf_addr(ldata, tail);
  143. if (n > size) {
  144. tty_audit_add_data(tty, from, size);
  145. memcpy(to, from, size);
  146. zero_buffer(tty, from, size);
  147. to += size;
  148. n -= size;
  149. from = ldata->read_buf;
  150. }
  151. tty_audit_add_data(tty, from, n);
  152. memcpy(to, from, n);
  153. zero_buffer(tty, from, n);
  154. }
  155. /**
  156. * n_tty_kick_worker - start input worker (if required)
  157. * @tty: terminal
  158. *
  159. * Re-schedules the flip buffer work if it may have stopped.
  160. *
  161. * Locking:
  162. * * Caller holds exclusive %termios_rwsem, or
  163. * * n_tty_read()/consumer path:
  164. * holds non-exclusive %termios_rwsem
  165. */
  166. static void n_tty_kick_worker(const struct tty_struct *tty)
  167. {
  168. struct n_tty_data *ldata = tty->disc_data;
  169. /* Did the input worker stop? Restart it */
  170. if (unlikely(READ_ONCE(ldata->no_room))) {
  171. WRITE_ONCE(ldata->no_room, 0);
  172. WARN_RATELIMIT(tty->port->itty == NULL,
  173. "scheduling with invalid itty\n");
  174. /* see if ldisc has been killed - if so, this means that
  175. * even though the ldisc has been halted and ->buf.work
  176. * cancelled, ->buf.work is about to be rescheduled
  177. */
  178. WARN_RATELIMIT(test_bit(TTY_LDISC_HALTED, &tty->flags),
  179. "scheduling buffer work for halted ldisc\n");
  180. tty_buffer_restart_work(tty->port);
  181. }
  182. }
  183. static ssize_t chars_in_buffer(const struct tty_struct *tty)
  184. {
  185. const struct n_tty_data *ldata = tty->disc_data;
  186. size_t head = ldata->icanon ? ldata->canon_head : ldata->commit_head;
  187. return head - ldata->read_tail;
  188. }
  189. /**
  190. * n_tty_write_wakeup - asynchronous I/O notifier
  191. * @tty: tty device
  192. *
  193. * Required for the ptys, serial driver etc. since processes that attach
  194. * themselves to the master and rely on ASYNC IO must be woken up.
  195. */
  196. static void n_tty_write_wakeup(struct tty_struct *tty)
  197. {
  198. clear_bit(TTY_DO_WRITE_WAKEUP, &tty->flags);
  199. kill_fasync(&tty->fasync, SIGIO, POLL_OUT);
  200. }
  201. static void n_tty_check_throttle(struct tty_struct *tty)
  202. {
  203. struct n_tty_data *ldata = tty->disc_data;
  204. /*
  205. * Check the remaining room for the input canonicalization
  206. * mode. We don't want to throttle the driver if we're in
  207. * canonical mode and don't have a newline yet!
  208. */
  209. if (ldata->icanon && ldata->canon_head == ldata->read_tail)
  210. return;
  211. do {
  212. tty_set_flow_change(tty, TTY_THROTTLE_SAFE);
  213. if (N_TTY_BUF_SIZE - read_cnt(ldata) >= TTY_THRESHOLD_THROTTLE)
  214. break;
  215. } while (!tty_throttle_safe(tty));
  216. __tty_set_flow_change(tty, 0);
  217. }
  218. static void n_tty_check_unthrottle(struct tty_struct *tty)
  219. {
  220. if (tty->driver->type == TTY_DRIVER_TYPE_PTY) {
  221. if (chars_in_buffer(tty) > TTY_THRESHOLD_UNTHROTTLE)
  222. return;
  223. n_tty_kick_worker(tty);
  224. tty_wakeup(tty->link);
  225. return;
  226. }
  227. /* If there is enough space in the read buffer now, let the
  228. * low-level driver know. We use chars_in_buffer() to
  229. * check the buffer, as it now knows about canonical mode.
  230. * Otherwise, if the driver is throttled and the line is
  231. * longer than TTY_THRESHOLD_UNTHROTTLE in canonical mode,
  232. * we won't get any more characters.
  233. */
  234. do {
  235. tty_set_flow_change(tty, TTY_UNTHROTTLE_SAFE);
  236. if (chars_in_buffer(tty) > TTY_THRESHOLD_UNTHROTTLE)
  237. break;
  238. n_tty_kick_worker(tty);
  239. } while (!tty_unthrottle_safe(tty));
  240. __tty_set_flow_change(tty, 0);
  241. }
  242. /**
  243. * put_tty_queue - add character to tty
  244. * @c: character
  245. * @ldata: n_tty data
  246. *
  247. * Add a character to the tty read_buf queue.
  248. *
  249. * Locking:
  250. * * n_tty_receive_buf()/producer path:
  251. * caller holds non-exclusive %termios_rwsem
  252. */
  253. static inline void put_tty_queue(u8 c, struct n_tty_data *ldata)
  254. {
  255. *read_buf_addr(ldata, ldata->read_head) = c;
  256. ldata->read_head++;
  257. }
  258. /**
  259. * reset_buffer_flags - reset buffer state
  260. * @ldata: line disc data to reset
  261. *
  262. * Reset the read buffer counters and clear the flags. Called from
  263. * n_tty_open() and n_tty_flush_buffer().
  264. *
  265. * Locking:
  266. * * caller holds exclusive %termios_rwsem, or
  267. * * (locking is not required)
  268. */
  269. static void reset_buffer_flags(struct n_tty_data *ldata)
  270. {
  271. ldata->read_head = ldata->canon_head = ldata->read_tail = 0;
  272. ldata->commit_head = 0;
  273. ldata->line_start = 0;
  274. ldata->erasing = 0;
  275. bitmap_zero(ldata->read_flags, N_TTY_BUF_SIZE);
  276. ldata->push = 0;
  277. ldata->lookahead_count = 0;
  278. }
  279. static void n_tty_packet_mode_flush(struct tty_struct *tty)
  280. {
  281. if (tty->link->ctrl.packet) {
  282. scoped_guard(spinlock_irqsave, &tty->ctrl.lock)
  283. tty->ctrl.pktstatus |= TIOCPKT_FLUSHREAD;
  284. wake_up_interruptible(&tty->link->read_wait);
  285. }
  286. }
  287. /**
  288. * n_tty_flush_buffer - clean input queue
  289. * @tty: terminal device
  290. *
  291. * Flush the input buffer. Called when the tty layer wants the buffer flushed
  292. * (eg at hangup) or when the %N_TTY line discipline internally has to clean
  293. * the pending queue (for example some signals).
  294. *
  295. * Holds %termios_rwsem to exclude producer/consumer while buffer indices are
  296. * reset.
  297. *
  298. * Locking: %ctrl.lock, exclusive %termios_rwsem
  299. */
  300. static void n_tty_flush_buffer(struct tty_struct *tty)
  301. {
  302. guard(rwsem_write)(&tty->termios_rwsem);
  303. reset_buffer_flags(tty->disc_data);
  304. n_tty_kick_worker(tty);
  305. if (tty->link)
  306. n_tty_packet_mode_flush(tty);
  307. }
  308. /**
  309. * is_utf8_continuation - utf8 multibyte check
  310. * @c: byte to check
  311. *
  312. * Returns: true if the utf8 character @c is a multibyte continuation
  313. * character. We use this to correctly compute the on-screen size of the
  314. * character when printing.
  315. */
  316. static inline int is_utf8_continuation(u8 c)
  317. {
  318. return (c & 0xc0) == 0x80;
  319. }
  320. /**
  321. * is_continuation - multibyte check
  322. * @c: byte to check
  323. * @tty: terminal device
  324. *
  325. * Returns: true if the utf8 character @c is a multibyte continuation character
  326. * and the terminal is in unicode mode.
  327. */
  328. static inline int is_continuation(u8 c, const struct tty_struct *tty)
  329. {
  330. return I_IUTF8(tty) && is_utf8_continuation(c);
  331. }
  332. /**
  333. * do_output_char - output one character
  334. * @c: character (or partial unicode symbol)
  335. * @tty: terminal device
  336. * @space: space available in tty driver write buffer
  337. *
  338. * This is a helper function that handles one output character (including
  339. * special characters like TAB, CR, LF, etc.), doing OPOST processing and
  340. * putting the results in the tty driver's write buffer.
  341. *
  342. * Note that Linux currently ignores TABDLY, CRDLY, VTDLY, FFDLY and NLDLY.
  343. * They simply aren't relevant in the world today. If you ever need them, add
  344. * them here.
  345. *
  346. * Returns: the number of bytes of buffer space used or -1 if no space left.
  347. *
  348. * Locking: should be called under the %output_lock to protect the column state
  349. * and space left in the buffer.
  350. */
  351. static int do_output_char(u8 c, struct tty_struct *tty, int space)
  352. {
  353. struct n_tty_data *ldata = tty->disc_data;
  354. int spaces;
  355. if (!space)
  356. return -1;
  357. switch (c) {
  358. case '\n':
  359. if (O_ONLRET(tty))
  360. ldata->column = 0;
  361. if (O_ONLCR(tty)) {
  362. if (space < 2)
  363. return -1;
  364. ldata->canon_column = ldata->column = 0;
  365. tty->ops->write(tty, "\r\n", 2);
  366. return 2;
  367. }
  368. ldata->canon_column = ldata->column;
  369. break;
  370. case '\r':
  371. if (O_ONOCR(tty) && ldata->column == 0)
  372. return 0;
  373. if (O_OCRNL(tty)) {
  374. c = '\n';
  375. if (O_ONLRET(tty))
  376. ldata->canon_column = ldata->column = 0;
  377. break;
  378. }
  379. ldata->canon_column = ldata->column = 0;
  380. break;
  381. case '\t':
  382. spaces = 8 - (ldata->column & 7);
  383. if (O_TABDLY(tty) == XTABS) {
  384. if (space < spaces)
  385. return -1;
  386. ldata->column += spaces;
  387. tty->ops->write(tty, " ", spaces);
  388. return spaces;
  389. }
  390. ldata->column += spaces;
  391. break;
  392. case '\b':
  393. if (ldata->column > 0)
  394. ldata->column--;
  395. break;
  396. default:
  397. if (!iscntrl(c)) {
  398. if (O_OLCUC(tty))
  399. c = toupper(c);
  400. if (!is_continuation(c, tty))
  401. ldata->column++;
  402. }
  403. break;
  404. }
  405. tty_put_char(tty, c);
  406. return 1;
  407. }
  408. /**
  409. * process_output - output post processor
  410. * @c: character (or partial unicode symbol)
  411. * @tty: terminal device
  412. *
  413. * Output one character with OPOST processing.
  414. *
  415. * Returns: -1 when the output device is full and the character must be
  416. * retried.
  417. *
  418. * Locking: %output_lock to protect column state and space left (also, this is
  419. *called from n_tty_write() under the tty layer write lock).
  420. */
  421. static int process_output(u8 c, struct tty_struct *tty)
  422. {
  423. struct n_tty_data *ldata = tty->disc_data;
  424. guard(mutex)(&ldata->output_lock);
  425. if (do_output_char(c, tty, tty_write_room(tty)) < 0)
  426. return -1;
  427. return 0;
  428. }
  429. /**
  430. * process_output_block - block post processor
  431. * @tty: terminal device
  432. * @buf: character buffer
  433. * @nr: number of bytes to output
  434. *
  435. * Output a block of characters with OPOST processing.
  436. *
  437. * This path is used to speed up block console writes, among other things when
  438. * processing blocks of output data. It handles only the simple cases normally
  439. * found and helps to generate blocks of symbols for the console driver and
  440. * thus improve performance.
  441. *
  442. * Returns: the number of characters output.
  443. *
  444. * Locking: %output_lock to protect column state and space left (also, this is
  445. * called from n_tty_write() under the tty layer write lock).
  446. */
  447. static ssize_t process_output_block(struct tty_struct *tty,
  448. const u8 *buf, unsigned int nr)
  449. {
  450. struct n_tty_data *ldata = tty->disc_data;
  451. unsigned int space, i;
  452. const u8 *cp;
  453. guard(mutex)(&ldata->output_lock);
  454. space = tty_write_room(tty);
  455. if (space == 0)
  456. return 0;
  457. if (nr > space)
  458. nr = space;
  459. for (i = 0, cp = buf; i < nr; i++, cp++) {
  460. u8 c = *cp;
  461. switch (c) {
  462. case '\n':
  463. if (O_ONLRET(tty))
  464. ldata->column = 0;
  465. if (O_ONLCR(tty))
  466. goto do_write;
  467. ldata->canon_column = ldata->column;
  468. break;
  469. case '\r':
  470. if (O_ONOCR(tty) && ldata->column == 0)
  471. goto do_write;
  472. if (O_OCRNL(tty))
  473. goto do_write;
  474. ldata->canon_column = ldata->column = 0;
  475. break;
  476. case '\t':
  477. goto do_write;
  478. case '\b':
  479. if (ldata->column > 0)
  480. ldata->column--;
  481. break;
  482. default:
  483. if (!iscntrl(c)) {
  484. if (O_OLCUC(tty))
  485. goto do_write;
  486. if (!is_continuation(c, tty))
  487. ldata->column++;
  488. }
  489. break;
  490. }
  491. }
  492. do_write:
  493. return tty->ops->write(tty, buf, i);
  494. }
  495. static int n_tty_process_echo_ops(struct tty_struct *tty, size_t *tail,
  496. int space)
  497. {
  498. struct n_tty_data *ldata = tty->disc_data;
  499. u8 op;
  500. /*
  501. * Since add_echo_byte() is called without holding output_lock, we
  502. * might see only portion of multi-byte operation.
  503. */
  504. if (MASK(ldata->echo_commit) == MASK(*tail + 1))
  505. return -ENODATA;
  506. /*
  507. * If the buffer byte is the start of a multi-byte operation, get the
  508. * next byte, which is either the op code or a control character value.
  509. */
  510. op = echo_buf(ldata, *tail + 1);
  511. switch (op) {
  512. case ECHO_OP_ERASE_TAB: {
  513. unsigned int num_chars, num_bs;
  514. if (MASK(ldata->echo_commit) == MASK(*tail + 2))
  515. return -ENODATA;
  516. num_chars = echo_buf(ldata, *tail + 2);
  517. /*
  518. * Determine how many columns to go back in order to erase the
  519. * tab. This depends on the number of columns used by other
  520. * characters within the tab area. If this (modulo 8) count is
  521. * from the start of input rather than from a previous tab, we
  522. * offset by canon column. Otherwise, tab spacing is normal.
  523. */
  524. if (!(num_chars & 0x80))
  525. num_chars += ldata->canon_column;
  526. num_bs = 8 - (num_chars & 7);
  527. if (num_bs > space)
  528. return -ENOSPC;
  529. space -= num_bs;
  530. while (num_bs--) {
  531. tty_put_char(tty, '\b');
  532. if (ldata->column > 0)
  533. ldata->column--;
  534. }
  535. *tail += 3;
  536. break;
  537. }
  538. case ECHO_OP_SET_CANON_COL:
  539. ldata->canon_column = ldata->column;
  540. *tail += 2;
  541. break;
  542. case ECHO_OP_MOVE_BACK_COL:
  543. if (ldata->column > 0)
  544. ldata->column--;
  545. *tail += 2;
  546. break;
  547. case ECHO_OP_START:
  548. /* This is an escaped echo op start code */
  549. if (!space)
  550. return -ENOSPC;
  551. tty_put_char(tty, ECHO_OP_START);
  552. ldata->column++;
  553. space--;
  554. *tail += 2;
  555. break;
  556. default:
  557. /*
  558. * If the op is not a special byte code, it is a ctrl char
  559. * tagged to be echoed as "^X" (where X is the letter
  560. * representing the control char). Note that we must ensure
  561. * there is enough space for the whole ctrl pair.
  562. */
  563. if (space < 2)
  564. return -ENOSPC;
  565. tty_put_char(tty, '^');
  566. tty_put_char(tty, op ^ 0100);
  567. ldata->column += 2;
  568. space -= 2;
  569. *tail += 2;
  570. break;
  571. }
  572. return space;
  573. }
  574. /**
  575. * __process_echoes - write pending echo characters
  576. * @tty: terminal device
  577. *
  578. * Write previously buffered echo (and other ldisc-generated) characters to the
  579. * tty.
  580. *
  581. * Characters generated by the ldisc (including echoes) need to be buffered
  582. * because the driver's write buffer can fill during heavy program output.
  583. * Echoing straight to the driver will often fail under these conditions,
  584. * causing lost characters and resulting mismatches of ldisc state information.
  585. *
  586. * Since the ldisc state must represent the characters actually sent to the
  587. * driver at the time of the write, operations like certain changes in column
  588. * state are also saved in the buffer and executed here.
  589. *
  590. * A circular fifo buffer is used so that the most recent characters are
  591. * prioritized. Also, when control characters are echoed with a prefixed "^",
  592. * the pair is treated atomically and thus not separated.
  593. *
  594. * Locking: callers must hold %output_lock.
  595. */
  596. static size_t __process_echoes(struct tty_struct *tty)
  597. {
  598. struct n_tty_data *ldata = tty->disc_data;
  599. unsigned int space, old_space;
  600. size_t tail;
  601. u8 c;
  602. old_space = space = tty_write_room(tty);
  603. tail = ldata->echo_tail;
  604. while (MASK(ldata->echo_commit) != MASK(tail)) {
  605. c = echo_buf(ldata, tail);
  606. if (c == ECHO_OP_START) {
  607. int ret = n_tty_process_echo_ops(tty, &tail, space);
  608. if (ret == -ENODATA)
  609. goto not_yet_stored;
  610. if (ret < 0)
  611. break;
  612. space = ret;
  613. } else {
  614. if (O_OPOST(tty)) {
  615. int retval = do_output_char(c, tty, space);
  616. if (retval < 0)
  617. break;
  618. space -= retval;
  619. } else {
  620. if (!space)
  621. break;
  622. tty_put_char(tty, c);
  623. space -= 1;
  624. }
  625. tail += 1;
  626. }
  627. }
  628. /* If the echo buffer is nearly full (so that the possibility exists
  629. * of echo overrun before the next commit), then discard enough
  630. * data at the tail to prevent a subsequent overrun */
  631. while (ldata->echo_commit > tail &&
  632. ldata->echo_commit - tail >= ECHO_DISCARD_WATERMARK) {
  633. if (echo_buf(ldata, tail) == ECHO_OP_START) {
  634. if (echo_buf(ldata, tail + 1) == ECHO_OP_ERASE_TAB)
  635. tail += 3;
  636. else
  637. tail += 2;
  638. } else
  639. tail++;
  640. }
  641. not_yet_stored:
  642. ldata->echo_tail = tail;
  643. return old_space - space;
  644. }
  645. static void commit_echoes(struct tty_struct *tty)
  646. {
  647. struct n_tty_data *ldata = tty->disc_data;
  648. size_t nr, old, echoed;
  649. size_t head;
  650. scoped_guard(mutex, &ldata->output_lock) {
  651. head = ldata->echo_head;
  652. ldata->echo_mark = head;
  653. old = ldata->echo_commit - ldata->echo_tail;
  654. /*
  655. * Process committed echoes if the accumulated # of bytes is over the threshold
  656. * (and try again each time another block is accumulated)
  657. */
  658. nr = head - ldata->echo_tail;
  659. if (nr < ECHO_COMMIT_WATERMARK || (nr % ECHO_BLOCK > old % ECHO_BLOCK))
  660. return;
  661. ldata->echo_commit = head;
  662. echoed = __process_echoes(tty);
  663. }
  664. if (echoed && tty->ops->flush_chars)
  665. tty->ops->flush_chars(tty);
  666. }
  667. static void process_echoes(struct tty_struct *tty)
  668. {
  669. struct n_tty_data *ldata = tty->disc_data;
  670. size_t echoed;
  671. if (ldata->echo_mark == ldata->echo_tail)
  672. return;
  673. scoped_guard(mutex, &ldata->output_lock) {
  674. ldata->echo_commit = ldata->echo_mark;
  675. echoed = __process_echoes(tty);
  676. }
  677. if (echoed && tty->ops->flush_chars)
  678. tty->ops->flush_chars(tty);
  679. }
  680. /* NB: echo_mark and echo_head should be equivalent here */
  681. static void flush_echoes(struct tty_struct *tty)
  682. {
  683. struct n_tty_data *ldata = tty->disc_data;
  684. if ((!L_ECHO(tty) && !L_ECHONL(tty)) ||
  685. ldata->echo_commit == ldata->echo_head)
  686. return;
  687. guard(mutex)(&ldata->output_lock);
  688. ldata->echo_commit = ldata->echo_head;
  689. __process_echoes(tty);
  690. }
  691. /**
  692. * add_echo_byte - add a byte to the echo buffer
  693. * @c: unicode byte to echo
  694. * @ldata: n_tty data
  695. *
  696. * Add a character or operation byte to the echo buffer.
  697. */
  698. static inline void add_echo_byte(u8 c, struct n_tty_data *ldata)
  699. {
  700. *echo_buf_addr(ldata, ldata->echo_head) = c;
  701. smp_wmb(); /* Matches smp_rmb() in echo_buf(). */
  702. ldata->echo_head++;
  703. }
  704. /**
  705. * echo_move_back_col - add operation to move back a column
  706. * @ldata: n_tty data
  707. *
  708. * Add an operation to the echo buffer to move back one column.
  709. */
  710. static void echo_move_back_col(struct n_tty_data *ldata)
  711. {
  712. add_echo_byte(ECHO_OP_START, ldata);
  713. add_echo_byte(ECHO_OP_MOVE_BACK_COL, ldata);
  714. }
  715. /**
  716. * echo_set_canon_col - add operation to set the canon column
  717. * @ldata: n_tty data
  718. *
  719. * Add an operation to the echo buffer to set the canon column to the current
  720. * column.
  721. */
  722. static void echo_set_canon_col(struct n_tty_data *ldata)
  723. {
  724. add_echo_byte(ECHO_OP_START, ldata);
  725. add_echo_byte(ECHO_OP_SET_CANON_COL, ldata);
  726. }
  727. /**
  728. * echo_erase_tab - add operation to erase a tab
  729. * @num_chars: number of character columns already used
  730. * @after_tab: true if num_chars starts after a previous tab
  731. * @ldata: n_tty data
  732. *
  733. * Add an operation to the echo buffer to erase a tab.
  734. *
  735. * Called by the eraser function, which knows how many character columns have
  736. * been used since either a previous tab or the start of input. This
  737. * information will be used later, along with canon column (if applicable), to
  738. * go back the correct number of columns.
  739. */
  740. static void echo_erase_tab(unsigned int num_chars, int after_tab,
  741. struct n_tty_data *ldata)
  742. {
  743. add_echo_byte(ECHO_OP_START, ldata);
  744. add_echo_byte(ECHO_OP_ERASE_TAB, ldata);
  745. /* We only need to know this modulo 8 (tab spacing) */
  746. num_chars &= 7;
  747. /* Set the high bit as a flag if num_chars is after a previous tab */
  748. if (after_tab)
  749. num_chars |= 0x80;
  750. add_echo_byte(num_chars, ldata);
  751. }
  752. /**
  753. * echo_char_raw - echo a character raw
  754. * @c: unicode byte to echo
  755. * @ldata: line disc data
  756. *
  757. * Echo user input back onto the screen. This must be called only when
  758. * L_ECHO(tty) is true. Called from the &tty_driver.receive_buf() path.
  759. *
  760. * This variant does not treat control characters specially.
  761. */
  762. static void echo_char_raw(u8 c, struct n_tty_data *ldata)
  763. {
  764. if (c == ECHO_OP_START) {
  765. add_echo_byte(ECHO_OP_START, ldata);
  766. add_echo_byte(ECHO_OP_START, ldata);
  767. } else {
  768. add_echo_byte(c, ldata);
  769. }
  770. }
  771. /**
  772. * echo_char - echo a character
  773. * @c: unicode byte to echo
  774. * @tty: terminal device
  775. *
  776. * Echo user input back onto the screen. This must be called only when
  777. * L_ECHO(tty) is true. Called from the &tty_driver.receive_buf() path.
  778. *
  779. * This variant tags control characters to be echoed as "^X" (where X is the
  780. * letter representing the control char).
  781. */
  782. static void echo_char(u8 c, const struct tty_struct *tty)
  783. {
  784. struct n_tty_data *ldata = tty->disc_data;
  785. if (c == ECHO_OP_START) {
  786. add_echo_byte(ECHO_OP_START, ldata);
  787. add_echo_byte(ECHO_OP_START, ldata);
  788. } else {
  789. if (L_ECHOCTL(tty) && iscntrl(c) && c != '\t')
  790. add_echo_byte(ECHO_OP_START, ldata);
  791. add_echo_byte(c, ldata);
  792. }
  793. }
  794. /**
  795. * finish_erasing - complete erase
  796. * @ldata: n_tty data
  797. */
  798. static inline void finish_erasing(struct n_tty_data *ldata)
  799. {
  800. if (ldata->erasing) {
  801. echo_char_raw('/', ldata);
  802. ldata->erasing = 0;
  803. }
  804. }
  805. /**
  806. * eraser - handle erase function
  807. * @c: character input
  808. * @tty: terminal device
  809. *
  810. * Perform erase and necessary output when an erase character is present in the
  811. * stream from the driver layer. Handles the complexities of UTF-8 multibyte
  812. * symbols.
  813. *
  814. * Locking: n_tty_receive_buf()/producer path:
  815. * caller holds non-exclusive %termios_rwsem
  816. */
  817. static void eraser(u8 c, const struct tty_struct *tty)
  818. {
  819. struct n_tty_data *ldata = tty->disc_data;
  820. enum { ERASE, WERASE, KILL } kill_type;
  821. size_t head;
  822. size_t cnt;
  823. int seen_alnums;
  824. if (ldata->read_head == ldata->canon_head) {
  825. /* process_output('\a', tty); */ /* what do you think? */
  826. return;
  827. }
  828. if (c == ERASE_CHAR(tty))
  829. kill_type = ERASE;
  830. else if (c == WERASE_CHAR(tty))
  831. kill_type = WERASE;
  832. else {
  833. if (!L_ECHO(tty)) {
  834. ldata->read_head = ldata->canon_head;
  835. return;
  836. }
  837. if (!L_ECHOK(tty) || !L_ECHOKE(tty) || !L_ECHOE(tty)) {
  838. ldata->read_head = ldata->canon_head;
  839. finish_erasing(ldata);
  840. echo_char(KILL_CHAR(tty), tty);
  841. /* Add a newline if ECHOK is on and ECHOKE is off. */
  842. if (L_ECHOK(tty))
  843. echo_char_raw('\n', ldata);
  844. return;
  845. }
  846. kill_type = KILL;
  847. }
  848. seen_alnums = 0;
  849. while (MASK(ldata->read_head) != MASK(ldata->canon_head)) {
  850. head = ldata->read_head;
  851. /* erase a single possibly multibyte character */
  852. do {
  853. head--;
  854. c = read_buf(ldata, head);
  855. } while (is_continuation(c, tty) &&
  856. MASK(head) != MASK(ldata->canon_head));
  857. /* do not partially erase */
  858. if (is_continuation(c, tty))
  859. break;
  860. if (kill_type == WERASE) {
  861. /* Equivalent to BSD's ALTWERASE. */
  862. if (isalnum(c) || c == '_')
  863. seen_alnums++;
  864. else if (seen_alnums)
  865. break;
  866. }
  867. cnt = ldata->read_head - head;
  868. ldata->read_head = head;
  869. if (L_ECHO(tty)) {
  870. if (L_ECHOPRT(tty)) {
  871. if (!ldata->erasing) {
  872. echo_char_raw('\\', ldata);
  873. ldata->erasing = 1;
  874. }
  875. /* if cnt > 1, output a multi-byte character */
  876. echo_char(c, tty);
  877. while (--cnt > 0) {
  878. head++;
  879. echo_char_raw(read_buf(ldata, head), ldata);
  880. echo_move_back_col(ldata);
  881. }
  882. } else if (kill_type == ERASE && !L_ECHOE(tty)) {
  883. echo_char(ERASE_CHAR(tty), tty);
  884. } else if (c == '\t') {
  885. unsigned int num_chars = 0;
  886. int after_tab = 0;
  887. size_t tail = ldata->read_head;
  888. /*
  889. * Count the columns used for characters
  890. * since the start of input or after a
  891. * previous tab.
  892. * This info is used to go back the correct
  893. * number of columns.
  894. */
  895. while (MASK(tail) != MASK(ldata->canon_head)) {
  896. tail--;
  897. c = read_buf(ldata, tail);
  898. if (c == '\t') {
  899. after_tab = 1;
  900. break;
  901. } else if (iscntrl(c)) {
  902. if (L_ECHOCTL(tty))
  903. num_chars += 2;
  904. } else if (!is_continuation(c, tty)) {
  905. num_chars++;
  906. }
  907. }
  908. echo_erase_tab(num_chars, after_tab, ldata);
  909. } else {
  910. if (iscntrl(c) && L_ECHOCTL(tty)) {
  911. echo_char_raw('\b', ldata);
  912. echo_char_raw(' ', ldata);
  913. echo_char_raw('\b', ldata);
  914. }
  915. if (!iscntrl(c) || L_ECHOCTL(tty)) {
  916. echo_char_raw('\b', ldata);
  917. echo_char_raw(' ', ldata);
  918. echo_char_raw('\b', ldata);
  919. }
  920. }
  921. }
  922. if (kill_type == ERASE)
  923. break;
  924. }
  925. if (ldata->read_head == ldata->canon_head && L_ECHO(tty))
  926. finish_erasing(ldata);
  927. }
  928. static void __isig(int sig, struct tty_struct *tty)
  929. {
  930. struct pid *tty_pgrp = tty_get_pgrp(tty);
  931. if (tty_pgrp) {
  932. kill_pgrp(tty_pgrp, sig, 1);
  933. put_pid(tty_pgrp);
  934. }
  935. }
  936. /**
  937. * isig - handle the ISIG optio
  938. * @sig: signal
  939. * @tty: terminal
  940. *
  941. * Called when a signal is being sent due to terminal input. Called from the
  942. * &tty_driver.receive_buf() path, so serialized.
  943. *
  944. * Performs input and output flush if !NOFLSH. In this context, the echo
  945. * buffer is 'output'. The signal is processed first to alert any current
  946. * readers or writers to discontinue and exit their i/o loops.
  947. *
  948. * Locking: %ctrl.lock
  949. */
  950. static void isig(int sig, struct tty_struct *tty)
  951. {
  952. struct n_tty_data *ldata = tty->disc_data;
  953. if (L_NOFLSH(tty)) {
  954. /* signal only */
  955. __isig(sig, tty);
  956. return;
  957. }
  958. /* signal and flush */
  959. up_read(&tty->termios_rwsem);
  960. scoped_guard(rwsem_write, &tty->termios_rwsem) {
  961. __isig(sig, tty);
  962. /* clear echo buffer */
  963. scoped_guard(mutex, &ldata->output_lock) {
  964. ldata->echo_head = ldata->echo_tail = 0;
  965. ldata->echo_mark = ldata->echo_commit = 0;
  966. }
  967. /* clear output buffer */
  968. tty_driver_flush_buffer(tty);
  969. /* clear input buffer */
  970. reset_buffer_flags(tty->disc_data);
  971. /* notify pty master of flush */
  972. if (tty->link)
  973. n_tty_packet_mode_flush(tty);
  974. }
  975. down_read(&tty->termios_rwsem);
  976. }
  977. /**
  978. * n_tty_receive_break - handle break
  979. * @tty: terminal
  980. *
  981. * An RS232 break event has been hit in the incoming bitstream. This can cause
  982. * a variety of events depending upon the termios settings.
  983. *
  984. * Locking: n_tty_receive_buf()/producer path:
  985. * caller holds non-exclusive termios_rwsem
  986. *
  987. * Note: may get exclusive %termios_rwsem if flushing input buffer
  988. */
  989. static void n_tty_receive_break(struct tty_struct *tty)
  990. {
  991. struct n_tty_data *ldata = tty->disc_data;
  992. if (I_IGNBRK(tty))
  993. return;
  994. if (I_BRKINT(tty)) {
  995. isig(SIGINT, tty);
  996. return;
  997. }
  998. if (I_PARMRK(tty)) {
  999. put_tty_queue('\377', ldata);
  1000. put_tty_queue('\0', ldata);
  1001. }
  1002. put_tty_queue('\0', ldata);
  1003. }
  1004. /**
  1005. * n_tty_receive_overrun - handle overrun reporting
  1006. * @tty: terminal
  1007. *
  1008. * Data arrived faster than we could process it. While the tty driver has
  1009. * flagged this the bits that were missed are gone forever.
  1010. *
  1011. * Called from the receive_buf path so single threaded. Does not need locking
  1012. * as num_overrun and overrun_time are function private.
  1013. */
  1014. static void n_tty_receive_overrun(const struct tty_struct *tty)
  1015. {
  1016. struct n_tty_data *ldata = tty->disc_data;
  1017. ldata->num_overrun++;
  1018. if (time_is_before_jiffies(ldata->overrun_time + HZ)) {
  1019. tty_warn(tty, "%u input overrun(s)\n", ldata->num_overrun);
  1020. ldata->overrun_time = jiffies;
  1021. ldata->num_overrun = 0;
  1022. }
  1023. }
  1024. /**
  1025. * n_tty_receive_parity_error - error notifier
  1026. * @tty: terminal device
  1027. * @c: character
  1028. *
  1029. * Process a parity error and queue the right data to indicate the error case
  1030. * if necessary.
  1031. *
  1032. * Locking: n_tty_receive_buf()/producer path:
  1033. * caller holds non-exclusive %termios_rwsem
  1034. */
  1035. static void n_tty_receive_parity_error(const struct tty_struct *tty,
  1036. u8 c)
  1037. {
  1038. struct n_tty_data *ldata = tty->disc_data;
  1039. if (I_INPCK(tty)) {
  1040. if (I_IGNPAR(tty))
  1041. return;
  1042. if (I_PARMRK(tty)) {
  1043. put_tty_queue('\377', ldata);
  1044. put_tty_queue('\0', ldata);
  1045. put_tty_queue(c, ldata);
  1046. } else
  1047. put_tty_queue('\0', ldata);
  1048. } else
  1049. put_tty_queue(c, ldata);
  1050. }
  1051. static void
  1052. n_tty_receive_signal_char(struct tty_struct *tty, int signal, u8 c)
  1053. {
  1054. isig(signal, tty);
  1055. if (I_IXON(tty))
  1056. start_tty(tty);
  1057. if (L_ECHO(tty)) {
  1058. echo_char(c, tty);
  1059. commit_echoes(tty);
  1060. } else
  1061. process_echoes(tty);
  1062. }
  1063. static bool n_tty_is_char_flow_ctrl(struct tty_struct *tty, u8 c)
  1064. {
  1065. return c == START_CHAR(tty) || c == STOP_CHAR(tty);
  1066. }
  1067. /**
  1068. * n_tty_receive_char_flow_ctrl - receive flow control chars
  1069. * @tty: terminal device
  1070. * @c: character
  1071. * @lookahead_done: lookahead has processed this character already
  1072. *
  1073. * Receive and process flow control character actions.
  1074. *
  1075. * In case lookahead for flow control chars already handled the character in
  1076. * advance to the normal receive, the actions are skipped during normal
  1077. * receive.
  1078. *
  1079. * Returns true if @c is consumed as flow-control character, the character
  1080. * must not be treated as normal character.
  1081. */
  1082. static bool n_tty_receive_char_flow_ctrl(struct tty_struct *tty, u8 c,
  1083. bool lookahead_done)
  1084. {
  1085. if (!n_tty_is_char_flow_ctrl(tty, c))
  1086. return false;
  1087. if (lookahead_done)
  1088. return true;
  1089. if (c == START_CHAR(tty)) {
  1090. start_tty(tty);
  1091. process_echoes(tty);
  1092. return true;
  1093. }
  1094. /* STOP_CHAR */
  1095. stop_tty(tty);
  1096. return true;
  1097. }
  1098. static void n_tty_receive_handle_newline(struct tty_struct *tty, u8 c)
  1099. {
  1100. struct n_tty_data *ldata = tty->disc_data;
  1101. set_bit(MASK(ldata->read_head), ldata->read_flags);
  1102. put_tty_queue(c, ldata);
  1103. smp_store_release(&ldata->canon_head, ldata->read_head);
  1104. kill_fasync(&tty->fasync, SIGIO, POLL_IN);
  1105. wake_up_interruptible_poll(&tty->read_wait, EPOLLIN | EPOLLRDNORM);
  1106. }
  1107. static bool n_tty_receive_char_canon(struct tty_struct *tty, u8 c)
  1108. {
  1109. struct n_tty_data *ldata = tty->disc_data;
  1110. if (c == ERASE_CHAR(tty) || c == KILL_CHAR(tty) ||
  1111. (c == WERASE_CHAR(tty) && L_IEXTEN(tty))) {
  1112. eraser(c, tty);
  1113. commit_echoes(tty);
  1114. return true;
  1115. }
  1116. if (c == LNEXT_CHAR(tty) && L_IEXTEN(tty)) {
  1117. ldata->lnext = 1;
  1118. if (L_ECHO(tty)) {
  1119. finish_erasing(ldata);
  1120. if (L_ECHOCTL(tty)) {
  1121. echo_char_raw('^', ldata);
  1122. echo_char_raw('\b', ldata);
  1123. commit_echoes(tty);
  1124. }
  1125. }
  1126. return true;
  1127. }
  1128. if (c == REPRINT_CHAR(tty) && L_ECHO(tty) && L_IEXTEN(tty)) {
  1129. size_t tail = ldata->canon_head;
  1130. finish_erasing(ldata);
  1131. echo_char(c, tty);
  1132. echo_char_raw('\n', ldata);
  1133. while (MASK(tail) != MASK(ldata->read_head)) {
  1134. echo_char(read_buf(ldata, tail), tty);
  1135. tail++;
  1136. }
  1137. commit_echoes(tty);
  1138. return true;
  1139. }
  1140. if (c == '\n') {
  1141. if (L_ECHO(tty) || L_ECHONL(tty)) {
  1142. echo_char_raw('\n', ldata);
  1143. commit_echoes(tty);
  1144. }
  1145. n_tty_receive_handle_newline(tty, c);
  1146. return true;
  1147. }
  1148. if (c == EOF_CHAR(tty)) {
  1149. c = __DISABLED_CHAR;
  1150. n_tty_receive_handle_newline(tty, c);
  1151. return true;
  1152. }
  1153. if ((c == EOL_CHAR(tty)) ||
  1154. (c == EOL2_CHAR(tty) && L_IEXTEN(tty))) {
  1155. /*
  1156. * XXX are EOL_CHAR and EOL2_CHAR echoed?!?
  1157. */
  1158. if (L_ECHO(tty)) {
  1159. /* Record the column of first canon char. */
  1160. if (ldata->canon_head == ldata->read_head)
  1161. echo_set_canon_col(ldata);
  1162. echo_char(c, tty);
  1163. commit_echoes(tty);
  1164. }
  1165. /*
  1166. * XXX does PARMRK doubling happen for
  1167. * EOL_CHAR and EOL2_CHAR?
  1168. */
  1169. if (c == '\377' && I_PARMRK(tty))
  1170. put_tty_queue(c, ldata);
  1171. n_tty_receive_handle_newline(tty, c);
  1172. return true;
  1173. }
  1174. return false;
  1175. }
  1176. static void n_tty_receive_char_special(struct tty_struct *tty, u8 c,
  1177. bool lookahead_done)
  1178. {
  1179. struct n_tty_data *ldata = tty->disc_data;
  1180. if (I_IXON(tty) && n_tty_receive_char_flow_ctrl(tty, c, lookahead_done))
  1181. return;
  1182. if (L_ISIG(tty)) {
  1183. if (c == INTR_CHAR(tty)) {
  1184. n_tty_receive_signal_char(tty, SIGINT, c);
  1185. return;
  1186. } else if (c == QUIT_CHAR(tty)) {
  1187. n_tty_receive_signal_char(tty, SIGQUIT, c);
  1188. return;
  1189. } else if (c == SUSP_CHAR(tty)) {
  1190. n_tty_receive_signal_char(tty, SIGTSTP, c);
  1191. return;
  1192. }
  1193. }
  1194. if (tty->flow.stopped && !tty->flow.tco_stopped && I_IXON(tty) && I_IXANY(tty)) {
  1195. start_tty(tty);
  1196. process_echoes(tty);
  1197. }
  1198. if (c == '\r') {
  1199. if (I_IGNCR(tty))
  1200. return;
  1201. if (I_ICRNL(tty))
  1202. c = '\n';
  1203. } else if (c == '\n' && I_INLCR(tty))
  1204. c = '\r';
  1205. if (ldata->icanon && n_tty_receive_char_canon(tty, c))
  1206. return;
  1207. if (L_ECHO(tty)) {
  1208. finish_erasing(ldata);
  1209. if (c == '\n')
  1210. echo_char_raw('\n', ldata);
  1211. else {
  1212. /* Record the column of first canon char. */
  1213. if (ldata->canon_head == ldata->read_head)
  1214. echo_set_canon_col(ldata);
  1215. echo_char(c, tty);
  1216. }
  1217. commit_echoes(tty);
  1218. }
  1219. /* PARMRK doubling check */
  1220. if (c == '\377' && I_PARMRK(tty))
  1221. put_tty_queue(c, ldata);
  1222. put_tty_queue(c, ldata);
  1223. }
  1224. /**
  1225. * n_tty_receive_char - perform processing
  1226. * @tty: terminal device
  1227. * @c: character
  1228. *
  1229. * Process an individual character of input received from the driver. This is
  1230. * serialized with respect to itself by the rules for the driver above.
  1231. *
  1232. * Locking: n_tty_receive_buf()/producer path:
  1233. * caller holds non-exclusive %termios_rwsem
  1234. * publishes canon_head if canonical mode is active
  1235. */
  1236. static void n_tty_receive_char(struct tty_struct *tty, u8 c)
  1237. {
  1238. struct n_tty_data *ldata = tty->disc_data;
  1239. if (tty->flow.stopped && !tty->flow.tco_stopped && I_IXON(tty) && I_IXANY(tty)) {
  1240. start_tty(tty);
  1241. process_echoes(tty);
  1242. }
  1243. if (L_ECHO(tty)) {
  1244. finish_erasing(ldata);
  1245. /* Record the column of first canon char. */
  1246. if (ldata->canon_head == ldata->read_head)
  1247. echo_set_canon_col(ldata);
  1248. echo_char(c, tty);
  1249. commit_echoes(tty);
  1250. }
  1251. /* PARMRK doubling check */
  1252. if (c == '\377' && I_PARMRK(tty))
  1253. put_tty_queue(c, ldata);
  1254. put_tty_queue(c, ldata);
  1255. }
  1256. static void n_tty_receive_char_closing(struct tty_struct *tty, u8 c,
  1257. bool lookahead_done)
  1258. {
  1259. if (I_ISTRIP(tty))
  1260. c &= 0x7f;
  1261. if (I_IUCLC(tty) && L_IEXTEN(tty))
  1262. c = tolower(c);
  1263. if (I_IXON(tty)) {
  1264. if (!n_tty_receive_char_flow_ctrl(tty, c, lookahead_done) &&
  1265. tty->flow.stopped && !tty->flow.tco_stopped && I_IXANY(tty) &&
  1266. c != INTR_CHAR(tty) && c != QUIT_CHAR(tty) &&
  1267. c != SUSP_CHAR(tty)) {
  1268. start_tty(tty);
  1269. process_echoes(tty);
  1270. }
  1271. }
  1272. }
  1273. static void
  1274. n_tty_receive_char_flagged(struct tty_struct *tty, u8 c, u8 flag)
  1275. {
  1276. switch (flag) {
  1277. case TTY_BREAK:
  1278. n_tty_receive_break(tty);
  1279. break;
  1280. case TTY_PARITY:
  1281. case TTY_FRAME:
  1282. n_tty_receive_parity_error(tty, c);
  1283. break;
  1284. case TTY_OVERRUN:
  1285. n_tty_receive_overrun(tty);
  1286. break;
  1287. default:
  1288. tty_err(tty, "unknown flag %u\n", flag);
  1289. break;
  1290. }
  1291. }
  1292. static void
  1293. n_tty_receive_char_lnext(struct tty_struct *tty, u8 c, u8 flag)
  1294. {
  1295. struct n_tty_data *ldata = tty->disc_data;
  1296. ldata->lnext = 0;
  1297. if (likely(flag == TTY_NORMAL)) {
  1298. if (I_ISTRIP(tty))
  1299. c &= 0x7f;
  1300. if (I_IUCLC(tty) && L_IEXTEN(tty))
  1301. c = tolower(c);
  1302. n_tty_receive_char(tty, c);
  1303. } else
  1304. n_tty_receive_char_flagged(tty, c, flag);
  1305. }
  1306. /* Caller must ensure count > 0 */
  1307. static void n_tty_lookahead_flow_ctrl(struct tty_struct *tty, const u8 *cp,
  1308. const u8 *fp, size_t count)
  1309. {
  1310. struct n_tty_data *ldata = tty->disc_data;
  1311. u8 flag = TTY_NORMAL;
  1312. ldata->lookahead_count += count;
  1313. if (!I_IXON(tty))
  1314. return;
  1315. while (count--) {
  1316. if (fp)
  1317. flag = *fp++;
  1318. if (likely(flag == TTY_NORMAL))
  1319. n_tty_receive_char_flow_ctrl(tty, *cp, false);
  1320. cp++;
  1321. }
  1322. }
  1323. static void
  1324. n_tty_receive_buf_real_raw(const struct tty_struct *tty, const u8 *cp,
  1325. size_t count)
  1326. {
  1327. struct n_tty_data *ldata = tty->disc_data;
  1328. /* handle buffer wrap-around by a loop */
  1329. for (unsigned int i = 0; i < 2; i++) {
  1330. size_t head = MASK(ldata->read_head);
  1331. size_t n = min(count, N_TTY_BUF_SIZE - head);
  1332. memcpy(read_buf_addr(ldata, head), cp, n);
  1333. ldata->read_head += n;
  1334. cp += n;
  1335. count -= n;
  1336. }
  1337. }
  1338. static void
  1339. n_tty_receive_buf_raw(struct tty_struct *tty, const u8 *cp, const u8 *fp,
  1340. size_t count)
  1341. {
  1342. struct n_tty_data *ldata = tty->disc_data;
  1343. u8 flag = TTY_NORMAL;
  1344. while (count--) {
  1345. if (fp)
  1346. flag = *fp++;
  1347. if (likely(flag == TTY_NORMAL))
  1348. put_tty_queue(*cp++, ldata);
  1349. else
  1350. n_tty_receive_char_flagged(tty, *cp++, flag);
  1351. }
  1352. }
  1353. static void
  1354. n_tty_receive_buf_closing(struct tty_struct *tty, const u8 *cp, const u8 *fp,
  1355. size_t count, bool lookahead_done)
  1356. {
  1357. u8 flag = TTY_NORMAL;
  1358. while (count--) {
  1359. if (fp)
  1360. flag = *fp++;
  1361. if (likely(flag == TTY_NORMAL))
  1362. n_tty_receive_char_closing(tty, *cp++, lookahead_done);
  1363. }
  1364. }
  1365. static void n_tty_receive_buf_standard(struct tty_struct *tty, const u8 *cp,
  1366. const u8 *fp, size_t count,
  1367. bool lookahead_done)
  1368. {
  1369. struct n_tty_data *ldata = tty->disc_data;
  1370. u8 flag = TTY_NORMAL;
  1371. while (count--) {
  1372. u8 c = *cp++;
  1373. if (fp)
  1374. flag = *fp++;
  1375. if (ldata->lnext) {
  1376. n_tty_receive_char_lnext(tty, c, flag);
  1377. continue;
  1378. }
  1379. if (unlikely(flag != TTY_NORMAL)) {
  1380. n_tty_receive_char_flagged(tty, c, flag);
  1381. continue;
  1382. }
  1383. if (I_ISTRIP(tty))
  1384. c &= 0x7f;
  1385. if (I_IUCLC(tty) && L_IEXTEN(tty))
  1386. c = tolower(c);
  1387. if (L_EXTPROC(tty)) {
  1388. put_tty_queue(c, ldata);
  1389. continue;
  1390. }
  1391. if (test_bit(c, ldata->char_map))
  1392. n_tty_receive_char_special(tty, c, lookahead_done);
  1393. else
  1394. n_tty_receive_char(tty, c);
  1395. }
  1396. }
  1397. static void __receive_buf(struct tty_struct *tty, const u8 *cp, const u8 *fp,
  1398. size_t count)
  1399. {
  1400. struct n_tty_data *ldata = tty->disc_data;
  1401. bool preops = I_ISTRIP(tty) || (I_IUCLC(tty) && L_IEXTEN(tty));
  1402. size_t la_count = min(ldata->lookahead_count, count);
  1403. if (ldata->real_raw)
  1404. n_tty_receive_buf_real_raw(tty, cp, count);
  1405. else if (ldata->raw || (L_EXTPROC(tty) && !preops))
  1406. n_tty_receive_buf_raw(tty, cp, fp, count);
  1407. else if (tty->closing && !L_EXTPROC(tty)) {
  1408. if (la_count > 0) {
  1409. n_tty_receive_buf_closing(tty, cp, fp, la_count, true);
  1410. cp += la_count;
  1411. if (fp)
  1412. fp += la_count;
  1413. count -= la_count;
  1414. }
  1415. if (count > 0)
  1416. n_tty_receive_buf_closing(tty, cp, fp, count, false);
  1417. } else {
  1418. if (la_count > 0) {
  1419. n_tty_receive_buf_standard(tty, cp, fp, la_count, true);
  1420. cp += la_count;
  1421. if (fp)
  1422. fp += la_count;
  1423. count -= la_count;
  1424. }
  1425. if (count > 0)
  1426. n_tty_receive_buf_standard(tty, cp, fp, count, false);
  1427. flush_echoes(tty);
  1428. if (tty->ops->flush_chars)
  1429. tty->ops->flush_chars(tty);
  1430. }
  1431. ldata->lookahead_count -= la_count;
  1432. if (ldata->icanon && !L_EXTPROC(tty))
  1433. return;
  1434. /* publish read_head to consumer */
  1435. smp_store_release(&ldata->commit_head, ldata->read_head);
  1436. if (read_cnt(ldata)) {
  1437. kill_fasync(&tty->fasync, SIGIO, POLL_IN);
  1438. wake_up_interruptible_poll(&tty->read_wait, EPOLLIN | EPOLLRDNORM);
  1439. }
  1440. }
  1441. /**
  1442. * n_tty_receive_buf_common - process input
  1443. * @tty: device to receive input
  1444. * @cp: input chars
  1445. * @fp: flags for each char (if %NULL, all chars are %TTY_NORMAL)
  1446. * @count: number of input chars in @cp
  1447. * @flow: enable flow control
  1448. *
  1449. * Called by the terminal driver when a block of characters has been received.
  1450. * This function must be called from soft contexts not from interrupt context.
  1451. * The driver is responsible for making calls one at a time and in order (or
  1452. * using flush_to_ldisc()).
  1453. *
  1454. * Returns: the # of input chars from @cp which were processed.
  1455. *
  1456. * In canonical mode, the maximum line length is 4096 chars (including the line
  1457. * termination char); lines longer than 4096 chars are truncated. After 4095
  1458. * chars, input data is still processed but not stored. Overflow processing
  1459. * ensures the tty can always receive more input until at least one line can be
  1460. * read.
  1461. *
  1462. * In non-canonical mode, the read buffer will only accept 4095 chars; this
  1463. * provides the necessary space for a newline char if the input mode is
  1464. * switched to canonical.
  1465. *
  1466. * Note it is possible for the read buffer to _contain_ 4096 chars in
  1467. * non-canonical mode: the read buffer could already contain the maximum canon
  1468. * line of 4096 chars when the mode is switched to non-canonical.
  1469. *
  1470. * Locking: n_tty_receive_buf()/producer path:
  1471. * claims non-exclusive %termios_rwsem
  1472. * publishes commit_head or canon_head
  1473. */
  1474. static size_t
  1475. n_tty_receive_buf_common(struct tty_struct *tty, const u8 *cp, const u8 *fp,
  1476. size_t count, bool flow)
  1477. {
  1478. struct n_tty_data *ldata = tty->disc_data;
  1479. size_t n, rcvd = 0;
  1480. int room, overflow;
  1481. guard(rwsem_read)(&tty->termios_rwsem);
  1482. do {
  1483. /*
  1484. * When PARMRK is set, each input char may take up to 3 chars
  1485. * in the read buf; reduce the buffer space avail by 3x
  1486. *
  1487. * If we are doing input canonicalization, and there are no
  1488. * pending newlines, let characters through without limit, so
  1489. * that erase characters will be handled. Other excess
  1490. * characters will be beeped.
  1491. *
  1492. * paired with store in *_copy_from_read_buf() -- guarantees
  1493. * the consumer has loaded the data in read_buf up to the new
  1494. * read_tail (so this producer will not overwrite unread data)
  1495. */
  1496. size_t tail = smp_load_acquire(&ldata->read_tail);
  1497. room = N_TTY_BUF_SIZE - (ldata->read_head - tail);
  1498. if (I_PARMRK(tty))
  1499. room = DIV_ROUND_UP(room, 3);
  1500. room--;
  1501. if (room <= 0) {
  1502. overflow = ldata->icanon && ldata->canon_head == tail;
  1503. if (overflow && room < 0)
  1504. ldata->read_head--;
  1505. room = overflow;
  1506. WRITE_ONCE(ldata->no_room, flow && !room);
  1507. } else
  1508. overflow = 0;
  1509. n = min_t(size_t, count, room);
  1510. if (!n)
  1511. break;
  1512. /* ignore parity errors if handling overflow */
  1513. if (!overflow || !fp || *fp != TTY_PARITY)
  1514. __receive_buf(tty, cp, fp, n);
  1515. cp += n;
  1516. if (fp)
  1517. fp += n;
  1518. count -= n;
  1519. rcvd += n;
  1520. } while (!test_bit(TTY_LDISC_CHANGING, &tty->flags));
  1521. tty->receive_room = room;
  1522. /* Unthrottle if handling overflow on pty */
  1523. if (tty->driver->type == TTY_DRIVER_TYPE_PTY) {
  1524. if (overflow) {
  1525. tty_set_flow_change(tty, TTY_UNTHROTTLE_SAFE);
  1526. tty_unthrottle_safe(tty);
  1527. __tty_set_flow_change(tty, 0);
  1528. }
  1529. } else
  1530. n_tty_check_throttle(tty);
  1531. if (unlikely(ldata->no_room)) {
  1532. /*
  1533. * Barrier here is to ensure to read the latest read_tail in
  1534. * chars_in_buffer() and to make sure that read_tail is not loaded
  1535. * before ldata->no_room is set.
  1536. */
  1537. smp_mb();
  1538. if (!chars_in_buffer(tty))
  1539. n_tty_kick_worker(tty);
  1540. }
  1541. return rcvd;
  1542. }
  1543. static void n_tty_receive_buf(struct tty_struct *tty, const u8 *cp,
  1544. const u8 *fp, size_t count)
  1545. {
  1546. n_tty_receive_buf_common(tty, cp, fp, count, false);
  1547. }
  1548. static size_t n_tty_receive_buf2(struct tty_struct *tty, const u8 *cp,
  1549. const u8 *fp, size_t count)
  1550. {
  1551. return n_tty_receive_buf_common(tty, cp, fp, count, true);
  1552. }
  1553. /**
  1554. * n_tty_set_termios - termios data changed
  1555. * @tty: terminal
  1556. * @old: previous data
  1557. *
  1558. * Called by the tty layer when the user changes termios flags so that the line
  1559. * discipline can plan ahead. This function cannot sleep and is protected from
  1560. * re-entry by the tty layer. The user is guaranteed that this function will
  1561. * not be re-entered or in progress when the ldisc is closed.
  1562. *
  1563. * Locking: Caller holds @tty->termios_rwsem
  1564. */
  1565. static void n_tty_set_termios(struct tty_struct *tty, const struct ktermios *old)
  1566. {
  1567. struct n_tty_data *ldata = tty->disc_data;
  1568. if (!old || (old->c_lflag ^ tty->termios.c_lflag) & (ICANON | EXTPROC)) {
  1569. bitmap_zero(ldata->read_flags, N_TTY_BUF_SIZE);
  1570. ldata->line_start = ldata->read_tail;
  1571. if (!L_ICANON(tty) || !read_cnt(ldata)) {
  1572. ldata->canon_head = ldata->read_tail;
  1573. ldata->push = 0;
  1574. } else {
  1575. set_bit(MASK(ldata->read_head - 1), ldata->read_flags);
  1576. ldata->canon_head = ldata->read_head;
  1577. ldata->push = 1;
  1578. }
  1579. ldata->commit_head = ldata->read_head;
  1580. ldata->erasing = 0;
  1581. ldata->lnext = 0;
  1582. }
  1583. ldata->icanon = (L_ICANON(tty) != 0);
  1584. if (I_ISTRIP(tty) || I_IUCLC(tty) || I_IGNCR(tty) ||
  1585. I_ICRNL(tty) || I_INLCR(tty) || L_ICANON(tty) ||
  1586. I_IXON(tty) || L_ISIG(tty) || L_ECHO(tty) ||
  1587. I_PARMRK(tty)) {
  1588. bitmap_zero(ldata->char_map, 256);
  1589. if (I_IGNCR(tty) || I_ICRNL(tty))
  1590. set_bit('\r', ldata->char_map);
  1591. if (I_INLCR(tty))
  1592. set_bit('\n', ldata->char_map);
  1593. if (L_ICANON(tty)) {
  1594. set_bit(ERASE_CHAR(tty), ldata->char_map);
  1595. set_bit(KILL_CHAR(tty), ldata->char_map);
  1596. set_bit(EOF_CHAR(tty), ldata->char_map);
  1597. set_bit('\n', ldata->char_map);
  1598. set_bit(EOL_CHAR(tty), ldata->char_map);
  1599. if (L_IEXTEN(tty)) {
  1600. set_bit(WERASE_CHAR(tty), ldata->char_map);
  1601. set_bit(LNEXT_CHAR(tty), ldata->char_map);
  1602. set_bit(EOL2_CHAR(tty), ldata->char_map);
  1603. if (L_ECHO(tty))
  1604. set_bit(REPRINT_CHAR(tty),
  1605. ldata->char_map);
  1606. }
  1607. }
  1608. if (I_IXON(tty)) {
  1609. set_bit(START_CHAR(tty), ldata->char_map);
  1610. set_bit(STOP_CHAR(tty), ldata->char_map);
  1611. }
  1612. if (L_ISIG(tty)) {
  1613. set_bit(INTR_CHAR(tty), ldata->char_map);
  1614. set_bit(QUIT_CHAR(tty), ldata->char_map);
  1615. set_bit(SUSP_CHAR(tty), ldata->char_map);
  1616. }
  1617. clear_bit(__DISABLED_CHAR, ldata->char_map);
  1618. ldata->raw = 0;
  1619. ldata->real_raw = 0;
  1620. } else {
  1621. ldata->raw = 1;
  1622. if ((I_IGNBRK(tty) || (!I_BRKINT(tty) && !I_PARMRK(tty))) &&
  1623. (I_IGNPAR(tty) || !I_INPCK(tty)) &&
  1624. (tty->driver->flags & TTY_DRIVER_REAL_RAW))
  1625. ldata->real_raw = 1;
  1626. else
  1627. ldata->real_raw = 0;
  1628. }
  1629. /*
  1630. * Fix tty hang when I_IXON(tty) is cleared, but the tty
  1631. * been stopped by STOP_CHAR(tty) before it.
  1632. */
  1633. if (!I_IXON(tty) && old && (old->c_iflag & IXON) && !tty->flow.tco_stopped) {
  1634. start_tty(tty);
  1635. process_echoes(tty);
  1636. }
  1637. /* The termios change make the tty ready for I/O */
  1638. wake_up_interruptible(&tty->write_wait);
  1639. wake_up_interruptible(&tty->read_wait);
  1640. }
  1641. /**
  1642. * n_tty_close - close the ldisc for this tty
  1643. * @tty: device
  1644. *
  1645. * Called from the terminal layer when this line discipline is being shut down,
  1646. * either because of a close or becsuse of a discipline change. The function
  1647. * will not be called while other ldisc methods are in progress.
  1648. */
  1649. static void n_tty_close(struct tty_struct *tty)
  1650. {
  1651. struct n_tty_data *ldata = tty->disc_data;
  1652. if (tty->link)
  1653. n_tty_packet_mode_flush(tty);
  1654. guard(rwsem_write)(&tty->termios_rwsem);
  1655. vfree(ldata);
  1656. tty->disc_data = NULL;
  1657. }
  1658. /**
  1659. * n_tty_open - open an ldisc
  1660. * @tty: terminal to open
  1661. *
  1662. * Called when this line discipline is being attached to the terminal device.
  1663. * Can sleep. Called serialized so that no other events will occur in parallel.
  1664. * No further open will occur until a close.
  1665. */
  1666. static int n_tty_open(struct tty_struct *tty)
  1667. {
  1668. struct n_tty_data *ldata;
  1669. /* Currently a malloc failure here can panic */
  1670. ldata = vzalloc(sizeof(*ldata));
  1671. if (!ldata)
  1672. return -ENOMEM;
  1673. ldata->overrun_time = jiffies;
  1674. mutex_init(&ldata->atomic_read_lock);
  1675. mutex_init(&ldata->output_lock);
  1676. tty->disc_data = ldata;
  1677. tty->closing = 0;
  1678. /* indicate buffer work may resume */
  1679. clear_bit(TTY_LDISC_HALTED, &tty->flags);
  1680. n_tty_set_termios(tty, NULL);
  1681. tty_unthrottle(tty);
  1682. return 0;
  1683. }
  1684. static inline int input_available_p(const struct tty_struct *tty, int poll)
  1685. {
  1686. const struct n_tty_data *ldata = tty->disc_data;
  1687. int amt = poll && !TIME_CHAR(tty) && MIN_CHAR(tty) ? MIN_CHAR(tty) : 1;
  1688. if (ldata->icanon && !L_EXTPROC(tty))
  1689. return ldata->canon_head != ldata->read_tail;
  1690. else
  1691. return ldata->commit_head - ldata->read_tail >= amt;
  1692. }
  1693. /**
  1694. * copy_from_read_buf - copy read data directly
  1695. * @tty: terminal device
  1696. * @kbp: data
  1697. * @nr: size of data
  1698. *
  1699. * Helper function to speed up n_tty_read(). It is only called when %ICANON is
  1700. * off; it copies characters straight from the tty queue.
  1701. *
  1702. * Returns: true if it successfully copied data, but there is still more data
  1703. * to be had.
  1704. *
  1705. * Locking:
  1706. * * called under the @ldata->atomic_read_lock sem
  1707. * * n_tty_read()/consumer path:
  1708. * caller holds non-exclusive %termios_rwsem;
  1709. * read_tail published
  1710. */
  1711. static bool copy_from_read_buf(const struct tty_struct *tty, u8 **kbp,
  1712. size_t *nr)
  1713. {
  1714. struct n_tty_data *ldata = tty->disc_data;
  1715. size_t n;
  1716. bool is_eof;
  1717. size_t head = smp_load_acquire(&ldata->commit_head);
  1718. size_t tail = MASK(ldata->read_tail);
  1719. n = min3(head - ldata->read_tail, N_TTY_BUF_SIZE - tail, *nr);
  1720. if (!n)
  1721. return false;
  1722. u8 *from = read_buf_addr(ldata, tail);
  1723. memcpy(*kbp, from, n);
  1724. is_eof = n == 1 && *from == EOF_CHAR(tty);
  1725. tty_audit_add_data(tty, from, n);
  1726. zero_buffer(tty, from, n);
  1727. smp_store_release(&ldata->read_tail, ldata->read_tail + n);
  1728. /* Turn single EOF into zero-length read */
  1729. if (L_EXTPROC(tty) && ldata->icanon && is_eof &&
  1730. head == ldata->read_tail)
  1731. return false;
  1732. *kbp += n;
  1733. *nr -= n;
  1734. /* If we have more to copy, let the caller know */
  1735. return head != ldata->read_tail;
  1736. }
  1737. /**
  1738. * canon_copy_from_read_buf - copy read data in canonical mode
  1739. * @tty: terminal device
  1740. * @kbp: data
  1741. * @nr: size of data
  1742. *
  1743. * Helper function for n_tty_read(). It is only called when %ICANON is on; it
  1744. * copies one line of input up to and including the line-delimiting character
  1745. * into the result buffer.
  1746. *
  1747. * Note: When termios is changed from non-canonical to canonical mode and the
  1748. * read buffer contains data, n_tty_set_termios() simulates an EOF push (as if
  1749. * C-d were input) _without_ the %DISABLED_CHAR in the buffer. This causes data
  1750. * already processed as input to be immediately available as input although a
  1751. * newline has not been received.
  1752. *
  1753. * Locking:
  1754. * * called under the %atomic_read_lock mutex
  1755. * * n_tty_read()/consumer path:
  1756. * caller holds non-exclusive %termios_rwsem;
  1757. * read_tail published
  1758. */
  1759. static bool canon_copy_from_read_buf(const struct tty_struct *tty, u8 **kbp,
  1760. size_t *nr)
  1761. {
  1762. struct n_tty_data *ldata = tty->disc_data;
  1763. size_t n, size, more, c;
  1764. size_t eol;
  1765. size_t tail, canon_head;
  1766. int found = 0;
  1767. /* N.B. avoid overrun if nr == 0 */
  1768. if (!*nr)
  1769. return false;
  1770. canon_head = smp_load_acquire(&ldata->canon_head);
  1771. n = min(*nr, canon_head - ldata->read_tail);
  1772. tail = MASK(ldata->read_tail);
  1773. size = min_t(size_t, tail + n, N_TTY_BUF_SIZE);
  1774. eol = find_next_bit(ldata->read_flags, size, tail);
  1775. more = n - (size - tail);
  1776. if (eol == N_TTY_BUF_SIZE && more) {
  1777. /* scan wrapped without finding set bit */
  1778. eol = find_first_bit(ldata->read_flags, more);
  1779. found = eol != more;
  1780. } else
  1781. found = eol != size;
  1782. n = eol - tail;
  1783. if (n > N_TTY_BUF_SIZE)
  1784. n += N_TTY_BUF_SIZE;
  1785. c = n + found;
  1786. if (!found || read_buf(ldata, eol) != __DISABLED_CHAR)
  1787. n = c;
  1788. tty_copy(tty, *kbp, tail, n);
  1789. *kbp += n;
  1790. *nr -= n;
  1791. if (found)
  1792. clear_bit(eol, ldata->read_flags);
  1793. smp_store_release(&ldata->read_tail, ldata->read_tail + c);
  1794. if (found) {
  1795. if (!ldata->push)
  1796. ldata->line_start = ldata->read_tail;
  1797. else
  1798. ldata->push = 0;
  1799. tty_audit_push();
  1800. return false;
  1801. }
  1802. /* No EOL found - do a continuation retry if there is more data */
  1803. return ldata->read_tail != canon_head;
  1804. }
  1805. /*
  1806. * If we finished a read at the exact location of an
  1807. * EOF (special EOL character that's a __DISABLED_CHAR)
  1808. * in the stream, silently eat the EOF.
  1809. */
  1810. static void canon_skip_eof(struct n_tty_data *ldata)
  1811. {
  1812. size_t tail, canon_head;
  1813. canon_head = smp_load_acquire(&ldata->canon_head);
  1814. tail = ldata->read_tail;
  1815. // No data?
  1816. if (tail == canon_head)
  1817. return;
  1818. // See if the tail position is EOF in the circular buffer
  1819. tail &= (N_TTY_BUF_SIZE - 1);
  1820. if (!test_bit(tail, ldata->read_flags))
  1821. return;
  1822. if (read_buf(ldata, tail) != __DISABLED_CHAR)
  1823. return;
  1824. // Clear the EOL bit, skip the EOF char.
  1825. clear_bit(tail, ldata->read_flags);
  1826. smp_store_release(&ldata->read_tail, ldata->read_tail + 1);
  1827. }
  1828. /**
  1829. * job_control - check job control
  1830. * @tty: tty
  1831. * @file: file handle
  1832. *
  1833. * Perform job control management checks on this @file/@tty descriptor and if
  1834. * appropriate send any needed signals and return a negative error code if
  1835. * action should be taken.
  1836. *
  1837. * Locking:
  1838. * * redirected write test is safe
  1839. * * current->signal->tty check is safe
  1840. * * ctrl.lock to safely reference @tty->ctrl.pgrp
  1841. */
  1842. static int job_control(struct tty_struct *tty, struct file *file)
  1843. {
  1844. /* Job control check -- must be done at start and after
  1845. every sleep (POSIX.1 7.1.1.4). */
  1846. /* NOTE: not yet done after every sleep pending a thorough
  1847. check of the logic of this change. -- jlc */
  1848. /* don't stop on /dev/console */
  1849. if (file->f_op->write_iter == redirected_tty_write)
  1850. return 0;
  1851. return __tty_check_change(tty, SIGTTIN);
  1852. }
  1853. /*
  1854. * We still hold the atomic_read_lock and the termios_rwsem, and can just
  1855. * continue to copy data.
  1856. */
  1857. static ssize_t n_tty_continue_cookie(struct tty_struct *tty, u8 *kbuf,
  1858. size_t nr, void **cookie)
  1859. {
  1860. struct n_tty_data *ldata = tty->disc_data;
  1861. u8 *kb = kbuf;
  1862. if (ldata->icanon && !L_EXTPROC(tty)) {
  1863. /*
  1864. * If we have filled the user buffer, see if we should skip an
  1865. * EOF character before releasing the lock and returning done.
  1866. */
  1867. if (!nr)
  1868. canon_skip_eof(ldata);
  1869. else if (canon_copy_from_read_buf(tty, &kb, &nr))
  1870. return kb - kbuf;
  1871. } else {
  1872. if (copy_from_read_buf(tty, &kb, &nr))
  1873. return kb - kbuf;
  1874. }
  1875. /* No more data - release locks and stop retries */
  1876. n_tty_kick_worker(tty);
  1877. n_tty_check_unthrottle(tty);
  1878. up_read(&tty->termios_rwsem);
  1879. mutex_unlock(&ldata->atomic_read_lock);
  1880. *cookie = NULL;
  1881. return kb - kbuf;
  1882. }
  1883. static int n_tty_wait_for_input(struct tty_struct *tty, struct file *file,
  1884. struct wait_queue_entry *wait, long *timeout)
  1885. {
  1886. if (test_bit(TTY_OTHER_CLOSED, &tty->flags))
  1887. return -EIO;
  1888. if (tty_hung_up_p(file))
  1889. return 0;
  1890. /*
  1891. * Abort readers for ttys which never actually get hung up.
  1892. * See __tty_hangup().
  1893. */
  1894. if (test_bit(TTY_HUPPING, &tty->flags))
  1895. return 0;
  1896. if (!*timeout)
  1897. return 0;
  1898. if (tty_io_nonblock(tty, file))
  1899. return -EAGAIN;
  1900. if (signal_pending(current))
  1901. return -ERESTARTSYS;
  1902. up_read(&tty->termios_rwsem);
  1903. *timeout = wait_woken(wait, TASK_INTERRUPTIBLE, *timeout);
  1904. down_read(&tty->termios_rwsem);
  1905. return 1;
  1906. }
  1907. /**
  1908. * n_tty_read - read function for tty
  1909. * @tty: tty device
  1910. * @file: file object
  1911. * @kbuf: kernelspace buffer pointer
  1912. * @nr: size of I/O
  1913. * @cookie: if non-%NULL, this is a continuation read
  1914. * @offset: where to continue reading from (unused in n_tty)
  1915. *
  1916. * Perform reads for the line discipline. We are guaranteed that the line
  1917. * discipline will not be closed under us but we may get multiple parallel
  1918. * readers and must handle this ourselves. We may also get a hangup. Always
  1919. * called in user context, may sleep.
  1920. *
  1921. * This code must be sure never to sleep through a hangup.
  1922. *
  1923. * Locking: n_tty_read()/consumer path:
  1924. * claims non-exclusive termios_rwsem;
  1925. * publishes read_tail
  1926. */
  1927. static ssize_t n_tty_read(struct tty_struct *tty, struct file *file, u8 *kbuf,
  1928. size_t nr, void **cookie, unsigned long offset)
  1929. {
  1930. struct n_tty_data *ldata = tty->disc_data;
  1931. u8 *kb = kbuf;
  1932. DEFINE_WAIT_FUNC(wait, woken_wake_function);
  1933. int minimum, time;
  1934. ssize_t retval;
  1935. long timeout;
  1936. bool packet;
  1937. size_t old_tail;
  1938. /* Is this a continuation of a read started earlier? */
  1939. if (*cookie)
  1940. return n_tty_continue_cookie(tty, kbuf, nr, cookie);
  1941. retval = job_control(tty, file);
  1942. if (retval < 0)
  1943. return retval;
  1944. /*
  1945. * Internal serialization of reads.
  1946. */
  1947. if (file->f_flags & O_NONBLOCK) {
  1948. if (!mutex_trylock(&ldata->atomic_read_lock))
  1949. return -EAGAIN;
  1950. } else {
  1951. if (mutex_lock_interruptible(&ldata->atomic_read_lock))
  1952. return -ERESTARTSYS;
  1953. }
  1954. down_read(&tty->termios_rwsem);
  1955. minimum = time = 0;
  1956. timeout = MAX_SCHEDULE_TIMEOUT;
  1957. if (!ldata->icanon) {
  1958. minimum = MIN_CHAR(tty);
  1959. if (minimum) {
  1960. time = (HZ / 10) * TIME_CHAR(tty);
  1961. } else {
  1962. timeout = (HZ / 10) * TIME_CHAR(tty);
  1963. minimum = 1;
  1964. }
  1965. }
  1966. packet = tty->ctrl.packet;
  1967. old_tail = ldata->read_tail;
  1968. add_wait_queue(&tty->read_wait, &wait);
  1969. while (nr) {
  1970. /* First test for status change. */
  1971. if (packet && tty->link->ctrl.pktstatus) {
  1972. u8 cs;
  1973. if (kb != kbuf)
  1974. break;
  1975. scoped_guard(spinlock_irq, &tty->link->ctrl.lock) {
  1976. cs = tty->link->ctrl.pktstatus;
  1977. tty->link->ctrl.pktstatus = 0;
  1978. }
  1979. *kb++ = cs;
  1980. nr--;
  1981. break;
  1982. }
  1983. if (!input_available_p(tty, 0)) {
  1984. up_read(&tty->termios_rwsem);
  1985. tty_buffer_flush_work(tty->port);
  1986. down_read(&tty->termios_rwsem);
  1987. if (!input_available_p(tty, 0)) {
  1988. int ret = n_tty_wait_for_input(tty, file, &wait,
  1989. &timeout);
  1990. if (ret <= 0) {
  1991. retval = ret;
  1992. break;
  1993. }
  1994. continue;
  1995. }
  1996. }
  1997. if (ldata->icanon && !L_EXTPROC(tty)) {
  1998. if (canon_copy_from_read_buf(tty, &kb, &nr))
  1999. goto more_to_be_read;
  2000. } else {
  2001. /* Deal with packet mode. */
  2002. if (packet && kb == kbuf) {
  2003. *kb++ = TIOCPKT_DATA;
  2004. nr--;
  2005. }
  2006. if (copy_from_read_buf(tty, &kb, &nr) && kb - kbuf >= minimum)
  2007. goto more_to_be_read;
  2008. }
  2009. n_tty_check_unthrottle(tty);
  2010. if (kb - kbuf >= minimum)
  2011. break;
  2012. if (time)
  2013. timeout = time;
  2014. }
  2015. if (old_tail != ldata->read_tail) {
  2016. /*
  2017. * Make sure no_room is not read in n_tty_kick_worker()
  2018. * before setting ldata->read_tail in copy_from_read_buf().
  2019. */
  2020. smp_mb();
  2021. n_tty_kick_worker(tty);
  2022. }
  2023. up_read(&tty->termios_rwsem);
  2024. remove_wait_queue(&tty->read_wait, &wait);
  2025. mutex_unlock(&ldata->atomic_read_lock);
  2026. if (kb - kbuf)
  2027. retval = kb - kbuf;
  2028. return retval;
  2029. more_to_be_read:
  2030. /*
  2031. * There is more to be had and we have nothing more to wait for, so
  2032. * let's mark us for retries.
  2033. *
  2034. * NOTE! We return here with both the termios_sem and atomic_read_lock
  2035. * still held, the retries will release them when done.
  2036. */
  2037. remove_wait_queue(&tty->read_wait, &wait);
  2038. *cookie = cookie;
  2039. return kb - kbuf;
  2040. }
  2041. /**
  2042. * n_tty_write - write function for tty
  2043. * @tty: tty device
  2044. * @file: file object
  2045. * @buf: userspace buffer pointer
  2046. * @nr: size of I/O
  2047. *
  2048. * Write function of the terminal device. This is serialized with respect to
  2049. * other write callers but not to termios changes, reads and other such events.
  2050. * Since the receive code will echo characters, thus calling driver write
  2051. * methods, the %output_lock is used in the output processing functions called
  2052. * here as well as in the echo processing function to protect the column state
  2053. * and space left in the buffer.
  2054. *
  2055. * This code must be sure never to sleep through a hangup.
  2056. *
  2057. * Locking: output_lock to protect column state and space left
  2058. * (note that the process_output*() functions take this lock themselves)
  2059. */
  2060. static ssize_t n_tty_write(struct tty_struct *tty, struct file *file,
  2061. const u8 *buf, size_t nr)
  2062. {
  2063. const u8 *b = buf;
  2064. DEFINE_WAIT_FUNC(wait, woken_wake_function);
  2065. ssize_t num, retval = 0;
  2066. /* Job control check -- must be done at start (POSIX.1 7.1.1.4). */
  2067. if (L_TOSTOP(tty) && file->f_op->write_iter != redirected_tty_write) {
  2068. retval = tty_check_change(tty);
  2069. if (retval)
  2070. return retval;
  2071. }
  2072. guard(rwsem_read)(&tty->termios_rwsem);
  2073. /* Write out any echoed characters that are still pending */
  2074. process_echoes(tty);
  2075. add_wait_queue(&tty->write_wait, &wait);
  2076. while (1) {
  2077. if (signal_pending(current)) {
  2078. retval = -ERESTARTSYS;
  2079. break;
  2080. }
  2081. if (tty_hung_up_p(file) || (tty->link && !tty->link->count)) {
  2082. retval = -EIO;
  2083. break;
  2084. }
  2085. if (O_OPOST(tty)) {
  2086. while (nr > 0) {
  2087. num = process_output_block(tty, b, nr);
  2088. if (num < 0) {
  2089. if (num == -EAGAIN)
  2090. break;
  2091. retval = num;
  2092. goto break_out;
  2093. }
  2094. b += num;
  2095. nr -= num;
  2096. if (nr == 0)
  2097. break;
  2098. if (process_output(*b, tty) < 0)
  2099. break;
  2100. b++; nr--;
  2101. }
  2102. if (tty->ops->flush_chars)
  2103. tty->ops->flush_chars(tty);
  2104. } else {
  2105. struct n_tty_data *ldata = tty->disc_data;
  2106. while (nr > 0) {
  2107. scoped_guard(mutex, &ldata->output_lock)
  2108. num = tty->ops->write(tty, b, nr);
  2109. if (num < 0) {
  2110. retval = num;
  2111. goto break_out;
  2112. }
  2113. if (!num)
  2114. break;
  2115. b += num;
  2116. nr -= num;
  2117. }
  2118. }
  2119. if (!nr)
  2120. break;
  2121. if (tty_io_nonblock(tty, file)) {
  2122. retval = -EAGAIN;
  2123. break;
  2124. }
  2125. up_read(&tty->termios_rwsem);
  2126. wait_woken(&wait, TASK_INTERRUPTIBLE, MAX_SCHEDULE_TIMEOUT);
  2127. down_read(&tty->termios_rwsem);
  2128. }
  2129. break_out:
  2130. remove_wait_queue(&tty->write_wait, &wait);
  2131. if (nr && tty->fasync)
  2132. set_bit(TTY_DO_WRITE_WAKEUP, &tty->flags);
  2133. return (b - buf) ? b - buf : retval;
  2134. }
  2135. /**
  2136. * n_tty_poll - poll method for N_TTY
  2137. * @tty: terminal device
  2138. * @file: file accessing it
  2139. * @wait: poll table
  2140. *
  2141. * Called when the line discipline is asked to poll() for data or for special
  2142. * events. This code is not serialized with respect to other events save
  2143. * open/close.
  2144. *
  2145. * This code must be sure never to sleep through a hangup.
  2146. *
  2147. * Locking: called without the kernel lock held -- fine.
  2148. */
  2149. static __poll_t n_tty_poll(struct tty_struct *tty, struct file *file,
  2150. poll_table *wait)
  2151. {
  2152. __poll_t mask = 0;
  2153. poll_wait(file, &tty->read_wait, wait);
  2154. poll_wait(file, &tty->write_wait, wait);
  2155. if (input_available_p(tty, 1))
  2156. mask |= EPOLLIN | EPOLLRDNORM;
  2157. else {
  2158. tty_buffer_flush_work(tty->port);
  2159. if (input_available_p(tty, 1))
  2160. mask |= EPOLLIN | EPOLLRDNORM;
  2161. }
  2162. if (tty->ctrl.packet && tty->link->ctrl.pktstatus)
  2163. mask |= EPOLLPRI | EPOLLIN | EPOLLRDNORM;
  2164. if (test_bit(TTY_OTHER_CLOSED, &tty->flags))
  2165. mask |= EPOLLHUP;
  2166. if (tty_hung_up_p(file))
  2167. mask |= EPOLLHUP;
  2168. if (tty->ops->write && !tty_is_writelocked(tty) &&
  2169. tty_chars_in_buffer(tty) < WAKEUP_CHARS &&
  2170. tty_write_room(tty) > 0)
  2171. mask |= EPOLLOUT | EPOLLWRNORM;
  2172. return mask;
  2173. }
  2174. static unsigned long inq_canon(struct n_tty_data *ldata)
  2175. {
  2176. size_t nr, head, tail;
  2177. if (ldata->canon_head == ldata->read_tail)
  2178. return 0;
  2179. head = ldata->canon_head;
  2180. tail = ldata->read_tail;
  2181. nr = head - tail;
  2182. /* Skip EOF-chars.. */
  2183. while (MASK(head) != MASK(tail)) {
  2184. if (test_bit(MASK(tail), ldata->read_flags) &&
  2185. read_buf(ldata, tail) == __DISABLED_CHAR)
  2186. nr--;
  2187. tail++;
  2188. }
  2189. return nr;
  2190. }
  2191. static int n_tty_ioctl(struct tty_struct *tty, unsigned int cmd,
  2192. unsigned long arg)
  2193. {
  2194. struct n_tty_data *ldata = tty->disc_data;
  2195. unsigned int num;
  2196. switch (cmd) {
  2197. case TIOCOUTQ:
  2198. return put_user(tty_chars_in_buffer(tty), (int __user *) arg);
  2199. case TIOCINQ:
  2200. scoped_guard(rwsem_write, &tty->termios_rwsem)
  2201. if (L_ICANON(tty) && !L_EXTPROC(tty))
  2202. num = inq_canon(ldata);
  2203. else
  2204. num = read_cnt(ldata);
  2205. return put_user(num, (unsigned int __user *) arg);
  2206. default:
  2207. return n_tty_ioctl_helper(tty, cmd, arg);
  2208. }
  2209. }
  2210. static struct tty_ldisc_ops n_tty_ops = {
  2211. .owner = THIS_MODULE,
  2212. .num = N_TTY,
  2213. .name = "n_tty",
  2214. .open = n_tty_open,
  2215. .close = n_tty_close,
  2216. .flush_buffer = n_tty_flush_buffer,
  2217. .read = n_tty_read,
  2218. .write = n_tty_write,
  2219. .ioctl = n_tty_ioctl,
  2220. .set_termios = n_tty_set_termios,
  2221. .poll = n_tty_poll,
  2222. .receive_buf = n_tty_receive_buf,
  2223. .write_wakeup = n_tty_write_wakeup,
  2224. .receive_buf2 = n_tty_receive_buf2,
  2225. .lookahead_buf = n_tty_lookahead_flow_ctrl,
  2226. };
  2227. /**
  2228. * n_tty_inherit_ops - inherit N_TTY methods
  2229. * @ops: struct tty_ldisc_ops where to save N_TTY methods
  2230. *
  2231. * Enables a 'subclass' line discipline to 'inherit' N_TTY methods.
  2232. */
  2233. void n_tty_inherit_ops(struct tty_ldisc_ops *ops)
  2234. {
  2235. *ops = n_tty_ops;
  2236. ops->owner = NULL;
  2237. }
  2238. EXPORT_SYMBOL_GPL(n_tty_inherit_ops);
  2239. void __init n_tty_init(void)
  2240. {
  2241. tty_register_ldisc(&n_tty_ops);
  2242. }