Massive stars shape their surroundings with mass loss from winds during their lifetimes. Fast ejecta from supernovae, from these massive stars, shocks this circumstellar medium. Emission generated by this interaction provides a window into the final stages of stellar evolution, by probing the history of mass loss from the progenitor. Here we use Chandra and Swift x-ray observations of the type II-P/L SN 2013ej to probe the history of mass loss from its progenitor. We model the observed x-rays as emission from both heated circumstellar matter and supernova ejecta. The circumstellar density profile probed by the supernova shock reveals a history of steady mass loss during the final 400 years. The inferred mass loss rate of $3 times 10^{-6} {rm ; M_odot ; yr^{-1}}$ points back to a 14 $M_odot$ progenitor. Soon after the explosion we find significant absorption of reverse shock emission by a cooling shell. The column depth of this shell observed in absorption provides an independent and consistent measurement of the circumstellar density seen in emission. We also determine the efficiency of cosmic ray acceleration from x-rays produced by Inverse Compton scattering of optical photons by relativistic electrons. Only about 1 percent of the thermal energy is used to accelerate electrons. Our x-ray observations and modeling provides stringent tests for models of massive stellar evolution and micro-physics of shocks.